Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

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
like image 541
Muhammad Umer Avatar asked Feb 13 '17 06:02

Muhammad Umer


People also ask

What does -- save dev do in npm?

The --save-dev option allows you to save packages under the devDependencies object in your package. json file. Any packages listed under the devDependencies will not be installed when you are in the production environment. For example, a test runner like karma won't be needed for the production build.

What is Save dev option?

The --save-dev option will save the package under devDependencies which is useful when installing only development packages that you may not want to ship in production.

Should I use -- save dev?

Using --save-dev is fine when you're writing a simple application, and it won't be used as a library. The problem comes along when you might have dependencies. If you stored your type declarations in your devDependencies , your consumers would not automatically get your type declarations.

Do I need -- save for npm?

You don't need --save anymore for NPM installs. This was long the golden standard to install a package and save it as a dependency in your project. Meaning if we didn't specify the --save flag, it would only get locally installed and not added to the package. json file.


2 Answers

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.

like image 74
pashute Avatar answered Sep 30 '22 00:09

pashute


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

like image 39
Max Koretskyi Avatar answered Sep 30 '22 00:09

Max Koretskyi