NPM Introduction
NPM stands for Node Package Manager. It is the default package manager for Node.js and helps developers install, manage, and share reusable JavaScript packages in their applications.
NPM makes application development easier by providing access to thousands of packages created by developers. These packages can help with tasks such as handling HTTP requests, validating data, building APIs, and managing application functionality.
1. What Is NPM?
NPM is a package management tool commonly used in Node.js projects. A package is reusable code that provides particular functionality. Instead of writing everything from scratch, developers can install packages and use them in their projects.
For example, a developer building a web application can use packages to create a server, handle routes, validate user input, or work with files.
Your application code
Installs and manages packages
Reusable code used by your application
2. Why Do We Use NPM?
NPM helps developers perform several important tasks:
- Install reusable JavaScript packages.
- Manage project dependencies.
- Record package information in
package.json. - Run application commands through NPM scripts.
- Install specific package versions.
- Share and reuse packages across projects.
3. Node.js and NPM
Node.js provides the JavaScript runtime that executes JavaScript outside a browser. NPM helps manage the packages used by Node.js applications.
| Node.js | NPM |
|---|---|
| Runs JavaScript code outside the browser. | Installs and manages packages. |
| Provides runtime capabilities. | Helps manage dependencies and scripts. |
Example command: node app.js |
Example command: npm install |
4. Check Node.js and NPM Versions
Before starting a Node.js project, check whether Node.js and NPM are available on your computer.
Check the Node.js version
node -v
Example output:
v22.0.0
Check the NPM version
npm -v
Example output:
10.5.0
These are example version numbers. The actual output depends on the versions installed on your computer.
5. What Is an NPM Package?
An NPM package is a collection of reusable code and related files that can be installed and used in a project.
For example, Express is a popular package used to build web servers and APIs with Node.js.
npm install express
This command installs the Express package in the current project.
After installation, a project may contain files like these:
MyProject/
|
|-- node_modules/
|-- package.json
|-- package-lock.json
|-- app.js
node_modulescontains installed package files.package.jsondescribes the project and its dependencies.package-lock.jsonrecords the resolved dependency versions.app.jscontains your application code.
6. Common NPM Commands
| Command | Purpose |
|---|---|
npm -v |
Displays the NPM version. |
npm init |
Starts an interactive project setup. |
npm init -y |
Creates a default package.json file. |
npm install |
Installs dependencies listed in the project manifest. |
npm install express |
Installs Express as a project dependency. |
npm uninstall express |
Removes Express from the project dependencies. |
npm list |
Displays the installed dependency tree. |
7. Create Your First NPM Project
Follow these steps to create a basic NPM project.
Step 1: Create a project folder
mkdir MyNpmProject
cd MyNpmProject
Step 2: Initialize the project
npm init -y
This command creates a package.json file with
default project information.
Step 3: Create app.js
console.log("Welcome to NPM!");
console.log("My first NPM project is running.");
Step 4: Run the application
node app.js
Expected output:
Welcome to NPM!
My first NPM project is running.
8. NPM Package Management Flow
npm init -y
npm install package-name
package.json and package-lock.json
9. Practice Tasks
- Check your installed Node.js version.
- Check your installed NPM version.
- Create a project folder named StudentApp.
- Initialize the project using npm init -y.
- Create app.js and print a welcome message.
- Explore the package.json file created by NPM.
Summary
NPM stands for Node Package Manager and helps developers install and manage reusable packages in Node.js applications. Commands such as npm init, npm install, and npm uninstall simplify project setup and dependency management. The package.json file stores project information and dependency details, while package-lock.json records resolved dependency versions. Understanding NPM provides a strong foundation for building and managing real-world Node.js applications at CIIT TRAINING INSTITUTE.