What do the --save flags do with npm install

I see instructions to install a package with either

npm install <package_name>

or

npm install <package_name> --save

or

npm install <package_name> --save-dev

What is the difference between these options?


Solution 1:

npm install <package_name> --save installs the package and updates the dependencies in your package.json. Since this question was asked there was a change to npm, such that --save has become the default option, so you do not need to use --save to update the dependencies.

npm install <package_name> --no-save installs the package but does not update the dependencies as listed in your package.json.

npm install <package_name> ---save-dev updates the devDependencies in your package. These are only used for local testing and development.

You can read more at https://docs.npmjs.com/getting-started/using-a-package.json.

Solution 2:

npm install takes 3 exclusive, optional flags which save or update the package version in your main package.json:

-S, --save: Package will appear in your dependencies.

-D, --save-dev: Package will appear in your devDependencies.

-O, --save-optional: Package will appear in your optionalDependencies.

When using any of the above options to save dependencies to your package.json, there is an additional, optional flag:

-E, --save-exact: Saved dependencies will be configured with an exact version rather than using npm's default semver range operator. Further, if you have an npm-shrinkwrap.json then it will be updated as well.

<scope> is optional. The package will be downloaded from the registry associated with the specified scope. If no registry is associated with the given scope the default registry is assumed. See npm-scope.

Note: if you do not include the @-symbol on your scope name, npm will interpret this as a GitHub repository instead, see below. Scopes names must also be followed by a slash.

Examples:

npm install sax --save
npm install githubname/reponame
npm install @myorg/privatepackage
npm install node-tap --save-dev
npm install dtrace-provider --save-optional
npm install readable-stream --save --save-exact

Note: If there is a file or folder named <name> in the current working directory, then it will try to install that, and only try to fetch the package by name if it is not valid.

(from official docs) https://docs.npmjs.com/cli/install

Solution 3:

The --save flag no longer serves a purpose.

Previously, as the other answers noted, the --save flag would update the dependencies in the project's package.json file, but npm install now includes this functionality by default.

At this point if you want to prevent npm install from saving dependencies, you have to use the --no-save flag.

Thanks to Coruscate5 for mentioning this in their comment.

More info in the npm-install documentation:

npm install saves any specified packages into dependencies by default. Additionally, you can control where and how they get saved with some additional flags:

-P, --save-prod: Package will appear in your dependencies. This is the default unless -D or -O are present.

-D, --save-dev: Package will appear in your devDependencies.

-O, --save-optional: Package will appear in your optionalDependencies.

--no-save: Prevents saving to dependencies.

When using any of the above options to save dependencies to your package.json, there are two additional, optional flags:

-E, --save-exact: Saved dependencies will be configured with an exact version rather than using npm’s default semver range operator.

-B, --save-bundle: Saved dependencies will also be added to your bundleDependencies list.