Installing Packages

npm install downloads packages into node_modules.

Syntaxnpm 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

Example · bash
# Add a package used by your app
npm install axios

# Install everything listed in package.json
npm install

# Remove a package
npm uninstall axios

When 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.

Example · bash
# 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 install

Clean install for CI

Uses `npm ci` for a fast, reproducible install in CI environments -- never modifies `package-lock.json`.

Example · bash
# npm ci installs exactly what's in package-lock.json
# and deletes node_modules first -- ideal for CI
npm ci

Install a specific version

Pins a specific version or range during install and verifies it with `npm list`.

Example · bash
# Exact version
npm install [email protected]

# Latest in a major range
npm install express@^4

# Check what was installed
npm list lodash

Discussion

  • Be the first to comment on this lesson.