Introduction
npm is the default package manager used with Node.js. It helps developers install, update, remove, and manage packages that add functionality to their applications. In this chapter, you will practice important npm concepts such as npm init, package.json, installing packages, local and global packages, npm scripts, dependencies, devDependencies, uninstalling packages, updating packages, and using npm commands in real Node.js projects. Node.js npm practice questions with solutions help to understand the concepts.
Question 1: How do you create a Node.js project using npm?
Problem
Create a new Node.js project using npm and generate a package.json file.
Solution
Open your terminal and create a folder:
mkdir npm-practice
Move into the folder:
cd npm-practice
Now run:
npm init
npm will ask several questions.
For example:
package name: npm-practice
version: 1.0.0
description: My npm practice project
entry point: index.js
test command:
git repository:
keywords:
author:
license: ISC
At the end, npm asks:
Is this OK? (yes)
Type:
yes
Output
A package.json file will be created:
npm-practice/
└── package.json
Example package.json
{
"name": "npm-practice",
"version": "1.0.0",
"description": "My npm practice project",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Step-by-Step Explanation
- Create a project folder.
- Open the folder in the terminal.
- Run
npm init. - npm asks questions about your project.
- Answer the questions.
- npm creates
package.json. package.jsonstores important information about your Node.js project.
Question 2: How do you install an npm package?
Problem
Install the popular lodash package in your Node.js project.
Solution
First create a project:
npm init -y
Then install lodash:
npm install lodash
Output
npm creates a node_modules folder and updates package.json.
Your project may now look like:
npm-practice/
├── node_modules/
├── package-lock.json
└── package.json
Your package.json will contain something similar to:
{
"name": "npm-practice",
"version": "1.0.0",
"dependencies": {
"lodash": "^4.17.21"
}
}
Step-by-Step Explanation
npm installis used to install packages.lodashis the package name.- npm downloads the package.
- The package is placed inside
node_modules. - npm adds the package to
dependencies. - npm also creates or updates
package-lock.json.
Question 3: How do you use an installed npm package?
Problem
Install lodash and use it in a Node.js program.
Solution
Install lodash:
npm install lodash
Create index.js:
const _ = require("lodash");
const numbers = [10, 20, 30, 40];
const total = _.sum(numbers);
console.log("Total:", total);
Run:
node index.js
Output
Total: 100
Step-by-Step Explanation
- Install lodash using npm.
- Node.js makes installed packages available to your application.
require("lodash")loads the package._.sum()calculates the total.- The result is displayed in the terminal.
Question 4: What is the difference between dependencies and devDependencies?
Problem
Install one package as a normal dependency and another package as a development dependency.
Solution
Install lodash normally:
npm install lodash
Install nodemon as a development dependency:
npm install --save-dev nodemon
Your package.json will contain something similar to:
{
"dependencies": {
"lodash": "^4.17.21"
},
"devDependencies": {
"nodemon": "^3.0.0"
}
}
Step-by-Step Explanation
dependencies contains packages required by your application when it runs.
Example:
lodash
devDependencies contains packages mainly needed while developing or testing your application.
Example:
nodemon
Simple Example
Think of a school project.
- Dependencies → things required to run the project.
- devDependencies → tools that help you build and test the project.
Question 5: How do you create and run npm scripts?
Problem
Create an npm script called start that runs your Node.js application.
Solution
Create index.js:
console.log("Node.js application started!");
Now open package.json and add:
{
"scripts": {
"start": "node index.js"
}
}
Run:
npm start
Output
Node.js application started!
Step-by-Step Explanation
- Create your Node.js file.
- Open
package.json. - Add a
scriptssection. - Create the
startscript. - Assign
node index.jsto it. - Run
npm start. - npm executes the command.
Another Example
You can create:
{
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js"
}
}
Then run:
npm start
or:
npm run dev
Question 6: How do you uninstall an npm package?
Problem
Install lodash and then remove it from your project.
Solution
First install:
npm install lodash
Now remove it:
npm uninstall lodash
Output
npm removes lodash from:
node_modules
and removes it from package.json.
Step-by-Step Explanation
- Install the package.
- npm adds it to your project.
- Run
npm uninstall lodash. - npm removes the package.
- The package is removed from the dependencies list.
General Syntax
npm uninstall package-name
For example:
npm uninstall express
Question 7: How do you update an npm package?
Problem
Check and update packages in your Node.js project.
Solution
First check outdated packages:
npm outdated
You may see output similar to:
Package Current Wanted Latest
lodash 4.17.20 4.17.21 4.17.21
To update packages according to the version ranges specified in package.json, run:
npm update
Step-by-Step Explanation
npm outdatedchecks for packages with newer versions.- It shows current and available versions.
npm updateupdates packages within the allowed version ranges.- Your
package-lock.jsonmay also be updated.
Question 8: How do you install a package globally?
Problem
Install a command-line npm package globally so that it can be used from different project folders.
Solution
For example, install nodemon globally:
npm install -g nodemon
Check whether it is available:
nodemon --version
Output
You should see a version number similar to:
3.x.x
The exact version depends on the current npm package version.
Step-by-Step Explanation
npm installnormally installs a package locally.- Adding
-gmeans global installation. - A globally installed command can be available outside one specific project.
nodemon --versionchecks whether the command is available.
Question 9: How do you install all packages from package.json?
Problem
Imagine you download an existing Node.js project from another computer. The project has a package.json, but the node_modules folder is missing. Install all required packages.
Solution
Move into the project directory:
cd my-project
Then run:
npm install
Output
npm reads:
package.json
and installs the required packages into:
node_modules
The project may become:
my-project/
├── node_modules/
├── package-lock.json
├── package.json
└── index.js
Step-by-Step Explanation
- Download or copy the project.
- Open its folder in the terminal.
- Make sure
package.jsonexists. - Run
npm install. - npm reads the dependencies.
- npm downloads the required packages.
- The
node_modulesfolder is created.
Question 10: How do you create a complete Node.js project using npm?
Problem
Create a small Node.js project using npm with:
package.json- Express
- npm scripts
- A development script
- A basic server
Solution
Step 1: Create the project
mkdir node-npm-project
Move into it:
cd node-npm-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 Node.js application!");
});
app.get("/about", (req, res) => {
res.send("This is the About page.");
});
app.listen(PORT, () => {
console.log(
`Server running at http://localhost:${PORT}`
);
});
Step 5: Add npm scripts
Update package.json:
{
"name": "node-npm-project",
"version": "1.0.0",
"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 be different when you install them.
Step 6: Start the application
For the normal start command:
npm start
For development:
npm run dev
Output
Terminal:
Server running at http://localhost:3000
Open:
http://localhost:3000/
Browser:
Welcome to my Node.js application!
Open:
http://localhost:3000/about
Browser:
This is the About page.
Step-by-Step Explanation
- Create a project folder.
- Run
npm init -y. - npm creates
package.json. - Install Express.
- Install Nodemon as a development dependency.
- Create
index.js. - Create application routes.
- Add
startanddevscripts. - Use
npm startfor the normal application. - Use
npm run devduring development. - Nodemon watches your files and can restart the application when changes are detected.
Key Takeaways
- npm is the package manager commonly used with Node.js.
npm initcreates apackage.jsonfile.npm init -ycreatespackage.jsonusing default values.package.jsoncontains project information and package dependencies.npm install package-nameinstalls a package locally.- Installed packages are generally stored in
node_modules. package-lock.jsonrecords the resolved dependency versions.dependenciescontains packages required by the application.devDependenciescontains packages used mainly during development.npm install --save-dev package-nameinstalls a development dependency.npm uninstall package-nameremoves a package.npm updateupdates packages within their declared version ranges.npm outdatedshows packages that may have newer versions available.npm installinstalls dependencies listed inpackage.json.- npm scripts allow you to create shortcuts for commands.
npm startruns thestartscript.npm run script-nameruns a custom npm script.- Global packages can be installed using
npm install -g. - Local project dependencies are usually preferred for application-specific packages and development tools.
- Understanding npm is essential for working with real-world Node.js projects.
FAQs
1. What is npm in Node.js?
npm stands for Node Package Manager. It is used to install, manage, update, and remove packages used by Node.js projects.
For example:
npm install express
installs the Express package in a project.
2. What is package.json?
package.json is an important configuration file in a Node.js project.
It can contain:
- Project name
- Version
- Description
- Main file
- Scripts
- Dependencies
- Development dependencies
Example:
{
"name": "my-project",
"version": "1.0.0",
"scripts": {
"start": "node index.js"
}
}
3. What is the difference between npm install and npm init?
npm init creates a new package.json file for a project.
npm init
npm install installs packages or installs the dependencies already listed in package.json.
npm install
They perform completely different jobs.
4. What is node_modules?
node_modules is the directory where npm stores packages installed for a project.
For example:
my-project/
├── node_modules/
├── package.json
└── index.js
You normally should not manually edit files inside node_modules.
5. What is package-lock.json?
package-lock.json records the exact dependency tree and resolved package versions installed for the project.
It helps npm reproduce a consistent installation across different environments.
For application projects, you generally commit package-lock.json to your version-control repository.
6. What is the difference between dependencies and devDependencies?
dependencies are packages your application needs when it runs.
Example:
"dependencies": {
"express": "..."
}
devDependencies are packages mainly used during development.
Example:
"devDependencies": {
"nodemon": "..."
}
7. What is the difference between local and global npm packages?
A local package is installed inside a particular project:
npm install package-name
A global package is installed for broader use on the computer:
npm install -g package-name
Local installation is generally preferred for project-specific dependencies because the project can keep track of the package version it needs.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
