Muhammad Umer
Muhammad Umer

Reputation: 18159

What's the difference between --dev, --save, and --save-dev for npm?

My understanding is this:

npm install //Installs everything that is listed in package.json
npm install --production //install everything minus dev packages
npm install $package --save //installs and add it to package.json
npm install $package --dev //install and add it to package.json but under dev
npm install --save-dev //??? isn't same thing as --dev flag

maybe there is no such thing as

npm install $package --dev

Upvotes: 9

Views: 6615

Answers (2)

pashute
pashute

Reputation: 4063

In many of the internet answers found on various forums and in many documentations for component installation via npm, there is a --save mentioned.

Local Install (no need for --save, its the default)

Well, it turns out that if you are NOT using the -g flag, then you get the --save as the default (which is now --save-prod or -P for short). So all the following are the same:

npm i blabla 
npm install blabla
npm i blabla --save
npm install blabla
npm i blabla --save-prod
npm install blabla -P

What this command does is twosome.

  1. It installs the blabla package and all its dependencies if are missing or need upgrades. The location of installation is under your project in the node modules folder.
  2. It marks this package in package.json in a section called dependencies. So next time you do an npm install or yarn install, all the correct packages will be installed according to this list.

The global packages are expected to be installed in your user's node_modules folder for global packages. The global packages that you installed are not listed anywhere in your project. See the next section.

Global install (-g - no writing to package.json)

The following are equivalent one to the other, but this time they DO NOT write what they are doing into a package.json file, but rather work because they are "in the path":

npm i -g blabla
npm i blabla -g
npm i blabla --save-global

These in the line above, install blabla and all its dependencies if missing or need an upgrade, but do not write anything in your package.json file.

Developer/Development install:

Last but not least is the dev option. The following are all equivalent

npm i -D blabla
npm i blabla -D
npm install blabla --save-dev
npm install blabla -D

This does the following:

  1. It does install the blabla and all dependencies to a folder under your project called node modules. and

  2. It lists the blabla package and any other packages that blabla needs, inside the package.json but this time under a special section called Dev-Dependencies.

You can then run npm i (or yarn i) and now it depends. If you are packaging as a developer, everything gets installed as usual. (nothing to write in project.json because we just read everything exactly from the list in exactly that file!!)

But, if you install for production (not part of the scope of this answer how to do that) all the Dev packages will not be installed. They were things you wanted only for the development stage, like Linters that read and check your code for errors.

Upvotes: 9

Max Koretskyi
Max Koretskyi

Reputation: 105547

Quote from the npm install documentation:

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.

So it seems that there is no such option as npm install $package --dev

Upvotes: 6

Related Questions