Node.js npm Packages Practice Questions with Solutions

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

  1. Create a project folder.
  2. Run npm init -y.
  3. npm creates package.json.
  4. Run npm install lodash.
  5. npm downloads Lodash.
  6. npm stores it inside node_modules.
  7. 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

  1. Install Lodash using npm.
  2. Load Lodash using require().
  3. Create an array of numbers.
  4. Use _.max() to find the largest number.
  5. 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

  1. npm install installs the package.
  2. --save-dev tells npm that the package is a development dependency.
  3. npm adds Nodemon to devDependencies.
  4. 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

  1. Open your project folder.
  2. Make sure packages have been installed.
  3. Run npm list.
  4. npm displays the dependency tree.
  5. --depth=0 shows 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

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

  1. Install two packages with one command.
  2. npm downloads both packages.
  3. Both are added to dependencies.
  4. Load the packages in your JavaScript file.
  5. Use Lodash to calculate the total.
  6. 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

  1. Install Nodemon as a development dependency.
  2. Create your Node.js application.
  3. Add a dev script.
  4. The script runs nodemon index.js.
  5. Start it with npm run dev.
  6. 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

  1. Create a Node.js project.
  2. Initialize npm.
  3. Install Express.
  4. Install Nodemon as a development dependency.
  5. Create the server.
  6. Create the home route.
  7. Create the API route.
  8. Add npm scripts.
  9. Run the application with npm start.
  10. Use npm run dev while developing.
  11. npm manages the packages through package.json.
  12. 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-name installs a package locally.
  • Installed packages are stored in node_modules.
  • npm records project dependencies in package.json.
  • package-lock.json records the resolved dependency tree.
  • Multiple packages can be installed with one npm install command.
  • npm uninstall package-name removes a package.
  • npm list displays installed packages.
  • npm list --depth=0 provides a simpler view of direct packages.
  • npm outdated checks for available package updates.
  • npm update updates packages within their declared version ranges.
  • --save-dev installs a package as a development dependency.
  • -g installs a package globally.
  • Local packages are generally preferred for project-specific dependencies.
  • npm scripts provide convenient commands for running development tasks.
  • dependencies contain packages needed by the application.
  • devDependencies contain 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.

Scroll to Top