WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Node.js · Basic · question 5 of 100

What is npm (Node Package Manager) and how do you use it?

📕 Buy this interview preparation book: 100 Node.js questions & answers — PDF + EPUB for $5

npm (Node Package Manager) is the default package manager for the JavaScript runtime environment Node.js. It allows developers to share and reuse packages (pieces of code) from other developers easily, and manage dependencies for their own projects. The npm registry is a large public repository of open-source packages, and you can access it via the npm CLI (command line interface) or the npm website.

To use npm, you need to have Node.js installed on your system. You can download and install Node.js from the official website (https://nodejs.org/).

Once Node.js is installed, you can check the npm version by typing the following command in your terminal (or command prompt):

npm -v

To use npm in a project, follow these steps:

1. **Initialize your project:** Navigate to your project directory using the terminal and run the following command:

npm init

This will create a ‘package.json‘ file, which stores metadata about your project and its dependencies. You can set default values or provide custom inputs when prompted by the CLI.

2. **Search and install packages:** Find packages from the npm registry using the npm website (https://www.npmjs.com/), or the CLI command:

npm search package_name

To install a package, use the following command:

npm install package_name

This command will install the package and add it as a dependency in your ‘package.json‘ file.

For example, if you want to install the popular web framework package ‘express‘, run:

npm install express

3. **Import and use packages:** In your project’s JavaScript files, use the ‘require‘ function to import the installed package:

const express = require('express');

Now you can use the package features in your code. For example, you can create an Express app and start a server:

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
  res.send('Hello World!');
});

app.listen(port, () => {
  console.log(`App is listening at http://localhost:${port}`);
});

4. **Uninstall and update packages:** To uninstall or remove a package, use the following command:

npm uninstall package_name

To update a package to a newer version, run:

npm update package_name

5. **Managing development and global packages:** Use the ‘–save-dev‘ option when installing packages used only for development, like testing frameworks or linters:

npm install package_name --save-dev

To install a package globally, use the ‘-g‘ flag. Global packages are commonly used for command line tools or development utilities:

npm install -g package_name

That’s a basic overview of npm and its primary functions. For more advanced usage and features, you can refer to the official documentation at https://docs.npmjs.com/.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Node.js interview — then scores it.
📞 Practice Node.js — free 15 min
📕 Buy this interview preparation book: 100 Node.js questions & answers — PDF + EPUB for $5

All 100 Node.js questions · All topics