askvity

How to Use the npm install Command?

Published in npm commands 5 mins read

To use the npm install command, you typically open your terminal or command prompt, navigate to your project's directory, and type npm install. This command is fundamental for managing project dependencies in Node.js environments.

The npm install command is used to install packages from the npm registry. When executed without any arguments in a directory containing a package.json file, it installs all dependencies listed in that file. It downloads the package files and places them into a folder named node_modules within your project directory. It also creates or updates the package-lock.json file, which locks down the exact versions of the dependencies installed.

Common npm install Commands

There are several ways to use npm install depending on your needs:

Installing All Project Dependencies

This is the most common use case.

  • Command: npm install
  • Usage: Run this in the root directory of a project that has a package.json file.
  • Effect: Installs all packages listed in the dependencies and devDependencies sections of package.json.

Installing a Specific Package

You can install individual packages required for your project.

  • Command: npm install <package-name>
  • Usage: Installs the latest version of <package-name>.
  • Effect: Installs the package into node_modules. By default, it also saves the package and its version to your package.json's dependencies list.

Installing Development Dependencies

These are packages needed only for development, testing, or building your project (e.g., testing frameworks, bundlers).

  • Command: npm install <package-name> --save-dev or npm install <package-name> -D
  • Usage: Installs the package and adds it to the devDependencies section in package.json.

Installing Packages Globally

Some tools (like command-line interfaces - CLIs) are useful to have installed globally on your system rather than in a specific project.

  • Command: npm install <package-name> --global or npm install <package-name> -g
  • Usage: Installs the package in a system-wide location.
  • Effect: The package becomes available as a command-line tool from any directory in your terminal. Note: Global packages are not tied to a specific project's package.json.

The npm Installation Process

When you run npm install, especially in a project with existing dependencies, npm performs a series of steps. As described in the npm documentation (circa v6):

  1. Load the existing node_modules tree from disk: npm first checks what packages and versions are currently installed in your project's node_modules folder.
  2. Clone the tree: It creates an in-memory representation (a clone) of the current dependency tree.
  3. Fetch the package.json and assorted metadata and add it to the clone: npm reads your package.json file to understand the desired state of dependencies and incorporates this information into the in-memory tree.
  4. Walk the clone and add any missing dependencies: npm compares the desired state (from package.json) with the current state (from the disk and its modifications). It identifies packages that are needed but missing or installed at the wrong version.
  5. Dependencies will be added as close to the top as is possible: npm attempts to install dependencies in a flat structure within node_modules. Packages are placed in the top-level node_modules folder if their versions are compatible with the requirements of all packages that depend on them. This helps avoid duplication and simplifies the dependency tree. If there are version conflicts, conflicting versions are installed in nested node_modules folders of the requiring packages.

This process ensures that your project's dependencies meet the requirements specified in package.json and are installed efficiently.

Understanding node_modules and package.json

  • package.json: This file is the heart of a Node.js project. It lists the project's metadata, scripts, and crucially, its dependencies (dependencies and devDependencies). When you install a package using npm install <package-name>, npm updates this file to record the dependency.
  • node_modules: This directory is where npm physically stores all the installed packages that your project depends on. You should generally not commit this directory to version control (like Git); instead, rely on package.json and package-lock.json for reproducibility.

Common npm install Flags

Here's a quick look at some common flags you might use:

Flag Short Form Description Example
--save-dev -D Installs as a devDependency npm install jest --save-dev
--global -g Installs the package globally npm install live-server -g
--no-save Prevents saving to package.json npm install some-tool --no-save
--force -f Forces npm to fetch remote packages even if a local copy exists npm install --force

By understanding these commands and the underlying process, you can effectively manage the libraries and tools your Node.js projects rely on using npm install.

Related Articles