Introduction
npm packages are reusable pieces of code that help Node.js developers add features without building everything from scratch. In this chapter, you will practice installing, using, updating, removing, and managing npm packages. You will also learn the difference between local and global packages, dependencies and devDependencies, package versions, package scripts, and how to use popular packages in simple Node.js projects. Node.js npm Packages practice questions with solutions help to build concepts.
Question 1: How do you install an npm package in a Node.js project?
Problem
Create a Node.js project and install the lodash npm package.
Solution
Create a project:
mkdir npm-package-practice
cd npm-package-practice
Initialize npm:
npm init -y
Install Lodash:
npm install lodash
Output
Your project will contain:
npm-package-practice/
├── node_modules/
├── package-lock.json
└── package.json
package.json will contain a dependency similar to:
{
"dependencies": {
"lodash": "^4.17.21"
}
}
The exact version may differ when you install the package.
Step-by-Step Explanation
- Create a project folder.
- Run
npm init -y. - npm creates
package.json. - Run
npm install lodash. - npm downloads Lodash.
- npm stores it inside
node_modules. - npm adds Lodash to
dependencies.
Question 2: How do you use an installed npm package?
Problem
Install Lodash and use it to find the largest number from an array.
Solution
Install Lodash:
npm install lodash
Create index.js:
const _ = require("lodash");
const numbers = [10, 45, 23, 89, 12];
const largest = _.max(numbers);
console.log("Largest number:", largest);
Run:
node index.js
Output
Largest number: 89
Step-by-Step Explanation
- Install Lodash using npm.
- Load Lodash using
require(). - Create an array of numbers.
- Use
_.max()to find the largest number. - Display the result.
Question 3: How do you install an npm package as a development dependency?
Problem
Install nodemon as a development dependency.
Solution
Run:
npm install --save-dev nodemon
You can also use the shorter form:
npm install -D nodemon
Output
Your package.json will contain:
{
"devDependencies": {
"nodemon": "^3.0.0"
}
}
The exact version may differ.
Step-by-Step Explanation
npm installinstalls the package.--save-devtells npm that the package is a development dependency.- npm adds Nodemon to
devDependencies. - Nodemon can then be used for development tasks.
Example
Add a script:
{
"scripts": {
"dev": "nodemon index.js"
}
}
Run:
npm run dev
Question 4: What is the difference between local and global npm packages?
Problem
Understand how local and global package installation works.
Solution
Install a package locally:
npm install lodash
Install a package globally:
npm install -g nodemon
Local Package
A local package belongs to a specific project.
Example:
my-project/
├── node_modules/
├── package.json
└── index.js
Global Package
A global package is installed at the system level and can provide commands that are available outside one particular project.
Check Nodemon:
nodemon --version
Step-by-Step Explanation
Local installation:
npm install package-name
Global installation:
npm install -g package-name
Question 5: How do you check installed npm packages?
Problem
Find out which packages are installed in your Node.js project.
Solution
Run:
npm list
For a simpler top-level view:
npm list --depth=0
Example Output
npm-package-practice@1.0.0
└── lodash@4.17.21
The exact version may differ.
Step-by-Step Explanation
- Open your project folder.
- Make sure packages have been installed.
- Run
npm list. - npm displays the dependency tree.
--depth=0shows mainly the packages directly installed by your project.
Question 6: How do you check outdated npm packages?
Problem
Check whether the npm packages in your project have newer versions available.
Solution
Run:
npm outdated
Example Output
You may see something similar to:
Package Current Wanted Latest
lodash 4.17.20 4.17.21 4.17.21
The actual output depends on the packages and versions in your project.
Step-by-Step Explanation
npm outdated helps you compare:
- Current version
- Wanted version
- Latest version
For example:
Current → version currently installed
Wanted → newest version allowed by package.json
Latest → newest published version
Question 7: How do you remove an npm package?
Problem
Install Lodash and then remove it from your project.
Solution
Install:
npm install lodash
Remove:
npm uninstall lodash
Output
Lodash is removed from the project’s dependencies and its installed package files are removed from node_modules.
Before
{
"dependencies": {
"lodash": "^4.17.21"
}
}
After
The Lodash entry is removed.
Step-by-Step Explanation
- Install the package.
- npm adds it to
dependencies. - Run
npm uninstall lodash. - npm removes the package.
- npm updates
package.json. - npm updates
package-lock.json.
Question 8: How do you use multiple npm packages in one project?
Problem
Install Lodash and Chalk and use both packages in the same Node.js application.
Solution
Install the packages:
npm install lodash chalk
Create index.js:
const _ = require("lodash");
const chalk = require("chalk");
const numbers = [10, 20, 30, 40];
const total = _.sum(numbers);
console.log(chalk.green("Total: " + total));
Output
Total: 100
The terminal text may appear styled depending on your terminal and the installed Chalk version.
Step-by-Step Explanation
- Install two packages with one command.
- npm downloads both packages.
- Both are added to
dependencies. - Load the packages in your JavaScript file.
- Use Lodash to calculate the total.
- Use Chalk to style terminal output.
Question 9: How do you create an npm script for a package?
Problem
Install Nodemon and create a dev script that automatically restarts your Node.js application when files change.
Solution
Install Nodemon:
npm install --save-dev nodemon
Create index.js:
console.log("Application is running...");
Add the following to package.json:
{
"scripts": {
"dev": "nodemon index.js"
},
"devDependencies": {
"nodemon": "^3.0.0"
}
}
Then run:
npm run dev
Output
Application is running...
Nodemon will continue watching the project files.
Step-by-Step Explanation
- Install Nodemon as a development dependency.
- Create your Node.js application.
- Add a
devscript. - The script runs
nodemon index.js. - Start it with
npm run dev. - When you change a watched file, Nodemon can restart the application.
Question 10: How do you build a Node.js project using npm packages?
Problem
Create a small Node.js application using:
- Express
- Nodemon
- npm scripts
package.json- A simple API route
Solution
Step 1: Create the project
mkdir npm-api-project
cd npm-api-project
Initialize npm:
npm init -y
Step 2: Install Express
npm install express
Step 3: Install Nodemon
npm install --save-dev nodemon
Step 4: Create index.js
const express = require("express");
const app = express();
const PORT = 3000;
app.get("/", (req, res) => {
res.send("Welcome to my npm package project!");
});
app.get("/api/courses", (req, res) => {
const courses = [
"JavaScript",
"Node.js",
"Python"
];
res.json(courses);
});
app.listen(PORT, () => {
console.log(
`Server running at http://localhost:${PORT}`
);
});
Step 5: Add npm scripts
Update package.json:
{
"name": "npm-api-project",
"version": "1.0.0",
"description": "A Node.js API using npm packages",
"main": "index.js",
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
},
"dependencies": {
"express": "^5.0.0"
},
"devDependencies": {
"nodemon": "^3.0.0"
}
}
The exact package versions may differ when you install them.
Step 6: Start the application
Normal mode:
npm start
Development mode:
npm run dev
Output
Terminal:
Server running at http://localhost:3000
Open:
http://localhost:3000/
Output:
Welcome to my npm package project!
Open:
http://localhost:3000/api/courses
Output:
[
"JavaScript",
"Node.js",
"Python"
]
Step-by-Step Explanation
- Create a Node.js project.
- Initialize npm.
- Install Express.
- Install Nodemon as a development dependency.
- Create the server.
- Create the home route.
- Create the API route.
- Add npm scripts.
- Run the application with
npm start. - Use
npm run devwhile developing. - npm manages the packages through
package.json. - The installed packages are stored in
node_modules.
Key Takeaways
- npm packages are reusable pieces of code that add functionality to Node.js projects.
npm install package-nameinstalls a package locally.- Installed packages are stored in
node_modules. - npm records project dependencies in
package.json. package-lock.jsonrecords the resolved dependency tree.- Multiple packages can be installed with one
npm installcommand. npm uninstall package-nameremoves a package.npm listdisplays installed packages.npm list --depth=0provides a simpler view of direct packages.npm outdatedchecks for available package updates.npm updateupdates packages within their declared version ranges.--save-devinstalls a package as a development dependency.-ginstalls a package globally.- Local packages are generally preferred for project-specific dependencies.
- npm scripts provide convenient commands for running development tasks.
dependenciescontain packages needed by the application.devDependenciescontain packages mainly used during development.- Popular npm packages can help you build APIs, websites, command-line tools, and other Node.js applications.
- Always check package compatibility before upgrading important dependencies.
- Understanding npm packages is essential for building real-world Node.js applications.
FAQs
1. What is an npm package?
An npm package is a reusable piece of software that can be installed and used in a Node.js project.
For example:
npm install express
installs the Express package.
2. How do I install an npm package?
Use:
npm install package-name
For example:
npm install express
npm downloads the package and normally adds it to your project’s dependencies.
3. Where are npm packages installed?
Local npm packages are generally installed inside:
node_modules/
For example:
my-project/
├── node_modules/
├── package.json
└── index.js
4. What is the difference between dependencies and devDependencies?
dependencies contains packages required by the application.
Example:
"dependencies": {
"express": "^5.0.0"
}
devDependencies contains packages mainly needed during development.
Example:
"devDependencies": {
"nodemon": "^3.0.0"
}
5. How do I remove an npm package?
Use:
npm uninstall package-name
For example:
npm uninstall express
npm removes the package and updates the project’s dependency information.
6. How do I check which npm packages are installed?
Run:
npm list --depth=0
This shows the project’s direct dependencies without displaying the complete dependency tree.
7. Should I install npm packages globally or locally?
For packages used by a particular project, local installation is generally recommended:
npm install package-name
Global installation:
npm install -g package-name
is more appropriate when you specifically need a command-line tool available outside one particular project.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
