Node.js npm Practice Questions with Solutions

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

  1. Create a project folder.
  2. Open the folder in the terminal.
  3. Run npm init.
  4. npm asks questions about your project.
  5. Answer the questions.
  6. npm creates package.json.
  7. package.json stores 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

  1. npm install is used to install packages.
  2. lodash is the package name.
  3. npm downloads the package.
  4. The package is placed inside node_modules.
  5. npm adds the package to dependencies.
  6. 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

  1. Install lodash using npm.
  2. Node.js makes installed packages available to your application.
  3. require("lodash") loads the package.
  4. _.sum() calculates the total.
  5. 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

  1. Create your Node.js file.
  2. Open package.json.
  3. Add a scripts section.
  4. Create the start script.
  5. Assign node index.js to it.
  6. Run npm start.
  7. 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

  1. Install the package.
  2. npm adds it to your project.
  3. Run npm uninstall lodash.
  4. npm removes the package.
  5. 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

  1. npm outdated checks for packages with newer versions.
  2. It shows current and available versions.
  3. npm update updates packages within the allowed version ranges.
  4. Your package-lock.json may 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

  1. npm install normally installs a package locally.
  2. Adding -g means global installation.
  3. A globally installed command can be available outside one specific project.
  4. nodemon --version checks 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

  1. Download or copy the project.
  2. Open its folder in the terminal.
  3. Make sure package.json exists.
  4. Run npm install.
  5. npm reads the dependencies.
  6. npm downloads the required packages.
  7. The node_modules folder 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

  1. Create a project folder.
  2. Run npm init -y.
  3. npm creates package.json.
  4. Install Express.
  5. Install Nodemon as a development dependency.
  6. Create index.js.
  7. Create application routes.
  8. Add start and dev scripts.
  9. Use npm start for the normal application.
  10. Use npm run dev during development.
  11. 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 init creates a package.json file.
  • npm init -y creates package.json using default values.
  • package.json contains project information and package dependencies.
  • npm install package-name installs a package locally.
  • Installed packages are generally stored in node_modules.
  • package-lock.json records the resolved dependency versions.
  • dependencies contains packages required by the application.
  • devDependencies contains packages used mainly during development.
  • npm install --save-dev package-name installs a development dependency.
  • npm uninstall package-name removes a package.
  • npm update updates packages within their declared version ranges.
  • npm outdated shows packages that may have newer versions available.
  • npm install installs dependencies listed in package.json.
  • npm scripts allow you to create shortcuts for commands.
  • npm start runs the start script.
  • npm run script-name runs 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.

Scroll to Top