What does npm install --save do?

Understand npm’s default dependency saving, when --save still matters, and how development, exact-version, global, and no-save options differ.

Best practices
Illustration accompanying a Node.js development guide

npm install PACKAGE --save saves the installed package as a project dependency. Modern npm enables saving by default for ordinary installs, so a new dependency usually needs only npm install PACKAGE. The explicit flag remains useful when overriding a save=false configuration.

Save a project dependency

npm install lodash
# Explicitly enable saving:
npm install lodash --save

For a new entry under normal settings, npm records the package in dependencies and updates the lockfile. npm 5 made saving the default; older tutorials often include --save because earlier versions required it.

Existing dependency placement and configuration also matter. If you need to deliberately select the production category, use --save-prod or -P, rather than assuming every install command moves an existing development dependency.

Choose the intended category

Option Purpose
--save-prod, -P Save as a production dependency
--save-dev, -D Save as a development dependency
--save-optional, -O Save as an optional dependency
--save-exact, -E Save an exact version instead of the configured range prefix
--no-save Install without saving dependency changes to the manifest or lockfile
--global, -g Install in npm's global location rather than the project

For example:

npm install --save-dev typescript
npm install --save-exact lodash@4.17.21

The second command illustrates exact saving, not a recommendation that this particular version meets every project's needs. An exact direct dependency still has its own transitive dependencies; -E does not freeze the entire dependency graph. A committed lockfile records the resolved tree for the project.

Temporary tools and global packages

npm install PACKAGE --no-save can be useful for a temporary local experiment, but it leaves the project's declared setup unable to reproduce that extra package. Do not depend on it for a build that others must run. npm's save=false setting also prevents writing package-lock.json during that install.

For a one-off executable, npm exec or npx may be more appropriate. These commands can download and execute package code when necessary, so select the package and version deliberately. A global install makes a CLI available through the global executable location; it does not add a normal project dependency.

Inspect unexpected behavior

npm config get save
npm config get save-prefix
npm --version

Configuration can come from command-line options, environment variables, or npm configuration files. A default caret prefix allows versions within its semver range; a different save-prefix setting changes what npm writes. Review both package.json and package-lock.json after an intentional dependency change, then test the resulting tree.

See npm install and npm configuration.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting