Open In App

npm install command

Last Updated : 24 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The npm install command is one of the most commonly used commands in Node.js development. It allows developers to install dependencies from the package.json file as well as additional packages from the npm registry. This command simplifies the process of setting up a project managing dependencies and ensuring all necessary modules are available for development.

The npm install command is fundamental to the Node.js projects. It fetches and installs the packages listed in the package.json file or specific packages from the npm registry. Whether we're setting up a new project adding the new libraries or updating existing dependencies npm install is at the heart of the workflow.

The command can be used to:

  • Install all project dependencies defined in the package.json.
  • Install specific packages.
  • Update dependencies or ensure the environment matches the project’s configuration.
  • Understanding how npm install works and the various options it supports is crucial for effective project management.

Syntax: The basic syntax of the npm install command is

npm install [<package-name>] [--save-dev] [--global]
  • <package-name>: The name of the package we want to the install.
  • --save-dev: Installs the package as a development dependency.
  • --global (or -g): Installs the package globally so it can be used in the any project.

Default Behavior: If no package name is provided the npm install will install all dependencies listed in the package.json file.

npm install

Key Features of npm install

  • Automatic Dependency Installation: When running the npm install npm reads the package.json file and installs the required dependencies automatically.
  • Flexible Package Installation: We can install packages locally or globally.
  • Version Control: We can specify exact versions or version ranges for the packages to the ensure compatibility and stability across your project.
  • Peer and Dev Dependencies: The npm install can handle both the development dependencies and peer dependencies for the managing shared libraries.
  • Caching: The npm caches installed packages to the speed up future installations and reduce network load.

Installing Local and Global Packages

There are two types of the package installations in the npm: local and global.

Local Installation:

By default, npm install installs packages locally in the project under the node_modules folder. These packages are only available to the project where they are installed.

For example to install a local package:

npm install lodash

This will install lodash into the project’s node_modules folder and add it to the package.json file as the dependency.

Global Installation

The Global installations are used for the packages that need to be accessible from the anywhere on the system such as the command-line tools. When we install a package globally it can be used across the all projects.

To install a package globally use the --global or -g flag:

npm install -g nodemon

Now, nodemon will be available globally and we can use it in the any project.

Package Management with package.json

The package.json file is central to the managing dependencies in the Node.js project. It contains information about the project its dependencies and other metadata.

Installing All Dependencies

If your project has a package.json file with the listed dependencies running the following the command will install all the dependencies:

npm install

The npm will automatically install all the dependencies and place them in the node_modules folder.

Adding a New Dependency

To add a new dependency and save it to the package.json we can run:

npm install express --save

This will install the express package and automatically add it to the dependencies section in the package.json.

Adding Development Dependencies

The Development dependencies are packages that are only required during the development such as the testing frameworks or build tools. We can install these using the --save-dev flag:

npm install jest --save-dev

This will add jest to the devDependencies section of package.json.

Common Options of npm install

The npm install command supports several useful options to the enhance its functionality:

  • --save: The Adds the installed package to the dependencies section in the package.json.
  • --save-dev: The Adds the installed package to the devDependencies section used for the development purposes only.
  • -g or --global: Installs the package globally making it available system-wide.
  • --no-save: The Prevents adding the installed package to the package.json.
  • --legacy-peer-deps: Ignores peer dependency warnings and installs the package anyway.

Examples of Using npm install

Let’s look at some examples of the how to use npm install effectively.

Example 1: Installing All Project Dependencies

If we clone a project with the package.json file we can install all required dependencies with:

npm install

Output:

21
npm install command

Example 2: Installing a Specific Package

To install a specific package like lodash and add it to the dependencies list in the package.json:

npm install lodash

Output:

23
npm install command

Next Article

Similar Reads