LinuxCommandLibrary

npm-install

Install Node.js project dependencies

TLDR

Install dependencies listed in package.json

$ npm install
copy

Download a specific version of a package and add it to the list of dependencies in package.json
$ npm install [package_name]@[version]
copy

Download the latest version of a package and add it to the list of dev dependencies in package.json
$ npm install [package_name] [[-D|--save-dev]]
copy

Download the latest version of a package and install it globally
$ npm install [[-g|--global]] [package_name]
copy

SYNOPSIS

npm install [...]

PARAMETERS


    The name of the package to install. Multiple package names can be specified, separated by spaces. Can include version specifiers (e.g., `package@version` or `package@tag`).

-g, --global
    Installs the package globally, making it available to all projects on the system. Requires appropriate permissions (e.g., `sudo` on Unix-like systems).

--save
    Saves the package as a dependency in the `package.json` file. (Default behavior for local installs).

--save-dev
    Saves the package as a development dependency in the `package.json` file (under `devDependencies`). Used for tools needed during development but not at runtime (e.g., testing frameworks).

--save-optional
    Saves the package as an optional dependency in the `package.json` file.

--no-save
    Prevents saving to `package.json`.

--production
    Will not install modules listed in `devDependencies`.

--registry
    Specifies the registry to use for package retrieval.

--cache
    Specifies the cache directory to use.

--force
    Forces npm to fetch remote resources even if a local copy exists on disk.

DESCRIPTION

The `npm install` command is the core workhorse of the Node Package Manager (npm). Its primary function is to install packages and their dependencies into your project. When executed in a directory containing a `package.json` file, it reads the dependencies listed within and downloads the corresponding packages from the npm registry or a specified alternative location.

Beyond simply downloading, `npm install` also handles dependency resolution, ensuring compatible versions are installed and that conflicting dependencies are resolved. It creates a `node_modules` directory where these packages are stored and generates a `package-lock.json` file (or updates it) to maintain a record of the exact versions installed. This ensures reproducibility across different environments. It can install packages globally by the `-g` flag. It can install individual packages, a list of packages, or even specific versions. It is a fundamental command for managing dependencies in Node.js projects and is crucial for building, running, and deploying Node.js applications.

CAVEATS

Be cautious when installing packages globally, as it can lead to dependency conflicts between projects. It's generally recommended to use local installations whenever possible. Ensure that `package.json` and `package-lock.json` are properly managed in your version control system to maintain consistency across environments.

Also, ensure that your node and npm versions are compatible with the project requirements.

USING `PACKAGE.JSON`

The `npm install` command relies heavily on the `package.json` file. Ensure that this file is properly configured with accurate dependency information. Incorrect or missing entries can lead to installation failures or unexpected behavior.

LOCKFILES

The `package-lock.json` (or `npm-shrinkwrap.json` in older versions) file is crucial for ensuring deterministic builds. Always commit this file to your version control system.

INSTALL FROM TARBALL OR GIT URL

`npm install` also supports installing packages directly from a tarball file or a Git repository URL. For example: `npm install ` or `npm install `.

HISTORY

npm was created by Isaac Z. Schlueter and initially released in 2010. It was designed to simplify the process of managing dependencies for Node.js projects. Over time, it has become an indispensable tool for Node.js developers, with numerous updates and improvements to enhance its functionality and performance. It has evolved to support features like scoped packages, security auditing, and improved dependency resolution algorithms. Its widespread adoption has contributed significantly to the growth and maturity of the Node.js ecosystem.

SEE ALSO

Copied to clipboard