203

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?

Flip
  • 6,233
  • 7
  • 46
  • 75
Obromios
  • 15,408
  • 15
  • 72
  • 127

3 Answers3

229

Updated, 2019:

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.


Original Answer:

npm install <package_name> --save installs the package and updates the dependencies in your package.json.

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.

GROVER.
  • 4,071
  • 2
  • 19
  • 66
Obromios
  • 15,408
  • 15
  • 72
  • 127
  • 30
    This was the top answer for my google search on "npm --save flag". Please consider updating it with the correct information, i.e. that --save is now the default, and --no-save is required in order to not update the list of dependencies in package.json – Jonas Rosenqvist Jul 26 '19 at 13:02
  • I would like to read an article or blurb on the reasoning of why --no-save was the default until recently. More mistakes and inconsistent behavior in favor of what, disk space? – JoseHdez_2 Apr 04 '21 at 14:37
  • Thanks for updating the answer to reflect that --save is the default. I see a lot of tutorials and stuff out there where they use the flag and since I'm so new to npm I don't remember a time it wasn't the default, I started wondering what that was all about. ;) – Chris Walker Aug 16 '21 at 18:17
64

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

Tim
  • 5,435
  • 7
  • 42
  • 62
Orange WebDev
  • 743
  • 5
  • 10
  • 55
    While the `--save` option still appears to work, it is no longer required - the packages are now saved to dependencies **by default**. The logic is now inversed - if you **don't** want to save your package, you need to specify `--no-save` – Coruscate5 Sep 08 '17 at 16:47
  • Thanks, @Coruscate5 answered my question exactly. – Kon Jan 08 '19 at 15:14
52

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.

Community
  • 1
  • 1
2xj
  • 720
  • 6
  • 7