codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🟢

Node.js

5 / 63 topics
1Getting Started with Node.js2Node.js Installation3Hello World Program4Node.js Modules5Node Package Manager (NPM)
Tutorials/Node.js/Node Package Manager (NPM)
🟢Node.js

Node Package Manager (NPM)

Updated 2026-04-20
4 min read

Node Package Manager (NPM)

Node Package Manager, commonly known as NPM, is a fundamental tool for managing JavaScript packages and dependencies in Node.js projects. It provides a command-line interface that allows developers to easily install, update, and manage the libraries required by their applications.

Introduction to NPM

NPM was created to simplify the process of sharing and reusing code among developers. Before NPM, managing dependencies manually was cumbersome and error-prone. With NPM, developers can declare the packages they need in a package.json file and easily install them using a single command.

Key Features of NPM

  • Package Management: Easily manage project dependencies.
  • Version Control: Specify exact versions or ranges for package versions.
  • Script Execution: Run custom scripts defined in the package.json.
  • Registry: Access to a vast repository of packages and modules.

Setting Up NPM

To start using NPM, you need to have Node.js installed on your system. NPM is included by default with Node.js version 5.0.0 and above. You can check if NPM is installed by running the following command in your terminal:

npm -v

This command will display the installed version of NPM.

Creating a New Project

To create a new Node.js project, navigate to your desired directory and run the following command:

npm init

This command initializes a new Node.js project by creating a package.json file. You can choose to fill in details interactively or use the -y flag to accept default values.

npm init -y

Installing Packages

NPM allows you to install packages locally or globally. Local installations are specific to your project, while global installations are available system-wide.

Local Installation

To install a package locally, use the install command followed by the package name:

npm install <package-name>

For example, to install the popular Express framework, you would run:

npm install express

This command downloads the package and adds it to the node_modules directory. It also updates the dependencies section in your package.json.

Global Installation

To install a package globally, use the -g flag:

npm install -g <package-name>

For example, to install the nodemon tool globally, you would run:

npm install -g nodemon

This allows you to run nodemon from any directory in your terminal.

Managing Dependencies

NPM uses a package.json file to manage dependencies. This file contains metadata about your project and lists all the packages it depends on.

Example package.json

{
  "name": "my-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "express": "^4.17.1"
  }
}

Installing Dependencies from package.json

If you clone a project or receive a new codebase, you can install all the dependencies listed in the package.json file by running:

npm install

This command reads the dependencies section and installs all the required packages.

Versioning

NPM uses semantic versioning (SemVer) to manage package versions. A version number consists of three parts: major, minor, and patch (e.g., 1.2.3).

  • Major: Breaking changes.
  • Minor: Backward-compatible features.
  • Patch: Backward-compatible bug fixes.

You can specify version ranges in your package.json to control which versions of a package are acceptable:

"dependencies": {
  "express": "^4.17.1"
}

The caret (^) operator allows NPM to install any version that is compatible with the specified major version.

Running Scripts

NPM allows you to define custom scripts in your package.json file. These scripts can be executed using the npm run command.

Example scripts Section

"scripts": {
  "start": "node index.js",
  "dev": "nodemon index.js"
}

To run a script, use:

npm run <script-name>

For example, to start your application in development mode, you would run:

npm run dev

Best Practices

  1. Use package.json: Always define your project's dependencies and scripts in the package.json.
  2. Version Control: Commit your package.json and package-lock.json files to version control.
  3. Avoid Global Installs: Use local installs whenever possible to keep dependencies isolated to specific projects.
  4. Use Version Ranges: Specify version ranges to ensure compatibility while allowing for updates.
  5. Regular Updates: Keep your packages up-to-date by periodically running npm update.

Conclusion

NPM is an essential tool for Node.js development, providing a robust framework for managing project dependencies and scripts. By following best practices and leveraging NPM's features, you can streamline your development workflow and ensure that your projects are well-managed and maintainable.

This tutorial has covered the basics of using NPM in Node.js projects. For more advanced topics, such as private packages, workspaces, and monorepos, refer to the official NPM documentation.


PreviousNode.js ModulesNext Node.js File System

Recommended Gear

Node.js ModulesNode.js File System