NPM stands for Node Package Manager. It is commonly used to install, manage, and share packages that developers use in Node.js projects.
Packages provide reusable code that helps developers build applications without writing every feature from scratch. For example, Express.js helps us create web servers and APIs.
npm command from your terminal.
NPM is the package manager commonly used in Node.js projects. It provides a command-line tool and an online package registry containing reusable JavaScript packages.
Install packages created by other developers instead of implementing every feature yourself.
Keep track of the packages your application requires.
Define reusable commands in package.json,
such as commands to start an application or run tests.
Developers can publish reusable packages to the NPM registry.
First, make sure Node.js and NPM are installed on your computer. Open Command Prompt or a terminal and enter:
npm -v
10.9.2
This is an example version number. Your installed version may differ.
Create a folder named MyNpmProject and open it in
your code editor.
MyNpmProject/
Before installing project dependencies, you can initialize your project with the following command:
npm init
NPM asks questions about your project, such as its name, version,
description, and entry point. Your answers are used to create
a package.json file.
To create a default package.json without answering
each question, use:
npm init -y
MyNpmProject/
└── package.json
The package.json file contains information about your
project, including its name, version, scripts, and dependencies.
{
"name": "my-npm-project",
"version": "1.0.0",
"description": "My first NPM project",
"main": "app.js",
"scripts": {
"start": "node app.js"
}
}
This is a simplified example. NPM can create a similar file automatically when you initialize a project.
Let's install a package named lodash. Lodash provides
utility functions for working with JavaScript values and collections.
npm install lodash
After installation, your project will normally contain:
MyNpmProject/
├── node_modules/
├── package.json
└── package-lock.json
node_modules/ contains installed packages and their dependencies.package.json records project details and dependency declarations.package-lock.json records the resolved dependency versions for reproducible installations.node_modules directly. Use NPM commands to
manage your packages.
After installing Lodash, create a file named app.js
in your project folder.
const _ = require("lodash");
const numbers = [10, 20, 30, 40];
console.log(_.sum(numbers));
node app.js
100
require("lodash") loads the installed Lodash package in this CommonJS example.numbers stores an array of numbers._.sum(numbers) calculates the sum of the array values.console.log() displays the result.Some packages are needed during development but not when the application is running in production. These can be installed as development dependencies.
npm install --save-dev nodemon
Nodemon can restart a Node.js application when it detects file changes. It is commonly used during development.
If you no longer need a package, you can remove it from your project.
npm uninstall lodash
NPM removes the package and updates the project's dependency information.
| Command | Purpose |
|---|---|
npm -v |
Displays the NPM version. |
npm init |
Creates a project configuration interactively. |
npm init -y |
Creates a default package.json. |
npm install lodash |
Installs Lodash in the project. |
npm install |
Installs dependencies declared by the project. |
npm uninstall lodash |
Removes Lodash from the project. |
npm list |
Lists installed packages for the project. |
npm run start |
Runs the configured start script. |
npm -v.MyNpmProject.npm init -y.npm install lodash.app.js and calculate the sum of an array using Lodash.node app.js.npm uninstall.package.json file describes your project and its
dependencies.
1. What is the full form of NPM?
2. Which file stores project dependency information?
3. Which command installs a package named Lodash?