Installing Packages
npm install downloads packages into node_modules.
Syntax
npm install <package>npm install downloads a package and its own dependencies into the node_modules folder, and records it in package.json.
Local vs global
- Local (default) — installed into the project, for code you import.
- Global (
-g) — installed system-wide, for command-line tools.
The node_modules folder can get large — never commit it to git. Others recreate it by running npm install.
Example
# Add a package used by your app
npm install axios
# Install everything listed in package.json
npm install
# Remove a package
npm uninstall axiosWhen to use it
- A new developer clones the project repository and runs `npm install` to install all listed dependencies from `package.json` before starting the app.
- A CI pipeline runs `npm ci` instead of `npm install` to get a clean, reproducible install based on `package-lock.json` without altering it.
- A developer installs `express` as a production dependency and `nodemon` as a dev dependency to keep them separated in `package.json`.
More examples
Install production and dev dependencies
Shows the three most common install commands -- the `--save-dev` flag keeps tools out of production bundles.
# Install a production dependency
npm install express
# Install a dev-only dependency
npm install --save-dev nodemon
# Install all dependencies listed in package.json
npm installClean install for CI
Uses `npm ci` for a fast, reproducible install in CI environments -- never modifies `package-lock.json`.
# npm ci installs exactly what's in package-lock.json
# and deletes node_modules first -- ideal for CI
npm ciInstall a specific version
Pins a specific version or range during install and verifies it with `npm list`.
# Exact version
npm install [email protected]
# Latest in a major range
npm install express@^4
# Check what was installed
npm list lodash
Discussion