npm install command is used to install Node packages for your project under the
Most of the time, you have some packages that are needed only for development and testing purposes.
--save-dev option allows you to save packages under the
devDependencies object in your
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.
You can use the
--save-dev option when installing the package as shown below:
npm install karma --save-dev
Another package that may be put in
Most likely, you optimize the production build by removing whitespaces and unnecessary characters.
Prettier is not needed in the production environment because it formats your code for easy reading and uniformity.
You can also switch the
--save-dev option with the shorter
npm install -D prettier
The packages installed as
devDependencies will be ignored when you run
npm install with the
npm install --production
Suppose you have a
package.json file with the following dependencies:
npm install --production will get you the following output:
$ npm install --production
$ npm list my-app ├── firstname.lastname@example.org
├── UNMET DEPENDENCY karma@^6.3.20
└── UNMET DEPENDENCY prettier@^2.6.2
Packages under the
devDependencies will be ignored under two conditions:
- You run the
npm install --production command
- You run
npm install but you have
NODE_ENV environment variable set as
The install flag will be prioritized over the
NODE_ENV variable, which means you can override the variable using the
-D option is used to install and save your project dependencies under the
Packages installed under
devDependencies will be ignored when you run a production build. This will shorten the time for the build since you don’t install unnecessary packages.
Now you’ve learned how the
--save-dev option works. All the best! 👍