What is the difference between:
npm install [package_name]
and:
npm install [package_name] --save
and:
--save-dev
saves semver spec into "devDependencies" array in your package descriptor file, --save
saves it into "dependencies" instead.
--save-dev is used for modules used in development of the application,not require while running it in production envionment --save is used to add it in package.json and it is required for running of the application.
Example: express,body-parser,lodash,helmet,mysql all these are used while running the application use --save to put in dependencies while mocha,istanbul,chai,sonarqube-scanner all are used during development ,so put those in dev-dependencies .
npm link or npm install will also install the dev-dependency modules along with dependency modules in your project folder
Let me give you an example,
jest
for testing and they use mocha
. Do you want them to install jest
as well? Just To run your library?No. right? That's why they are in devDependencies
.
When someone does, npm i yourPackage
only the libraries required to RUN your library will be installed. Other libraries you used to bundle your code with or testing and mocking will not be installed because you put them in devDependencies
. Pretty neat right?
So, Why do the developers need to expose the devDependancies?
Let's say your package is an open source package and 100s of people are sending pull requests to your package. Then how they will test the package? They will git clone
your repo and when they would do an npm i
the dependencies as well as devDependencies.
Because they are not using your package. They are developing the package further, thus, in order to test your package they need to pass the existing test cases as well write new. So, they need to use your devDependencies
which contain all the testing/building/mocking libraries that YOU used.
By default, NPM simply installs a package under node_modules. When you're trying to install dependencies for your app/module, you would need to first install them, and then add them to the dependencies
section of your package.json
.
--save-dev
adds the third-party package to the package's development dependencies. It won't be installed when someone runs npm install
directly to install your package. It's typically only installed if someone clones your source repository first and then runs npm install
in it.
--save
adds the third-party package to the package's dependencies. It will be installed together with the package whenever someone runs npm install package
.
Dev dependencies are those dependencies that are only needed for developing the package. That can include test runners, compilers, packagers, etc.
Both types of dependencies are stored in the package's package.json
file. --save
adds to dependencies
, --save-dev
adds to devDependencies
npm install documentation can be referred here.
--
Please note that --save
is now the default option, since NPM 5. Therefore, it is not explicitly needed anymore. It is possible to run npm install
without the --save
to achieve the same result.
People use npm on production to do wicked cool stuff, Node.js is an example of this, so you don't want all your dev tools being run.
If you are using gulp (or similar) to create build files to put on your server then it doesn't really matter.
All explanations here are great, but lacking a very important thing: How do you install production dependencies only? (without the development dependencies).
We separate dependencies
from devDependencies
by using --save
or --save-dev
.
To install all we use:
npm i
To install only production packages we should use:
npm i --only=production