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
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.
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.
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.
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.
In many of the internet answers found on various forums and in many documentations for component installation via npm, there is a --save
mentioned.
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.
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.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.
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.
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:
It does install the blabla and all dependencies to a folder under your project called node modules
. and
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With