How to install only "devDependencies" using npm

node.jsNpmNpm Installpackage.json

node.js Problem Overview


I am trying to install ONLY the "devDependencies" listed in my package.json file. But none of the following commands work as I expect. All of the following commands install the production dependencies also which I do not want.

npm install --dev
npm install --only=dev
npm install --only-dev

I cannot think of any more ways of telling the npm to install the devDependencies alone. :(

node.js Solutions


Solution 1 - node.js

Check the NPM docs for install:

> With the --production flag (or when the NODE_ENV environment variable is set to production), npm will not install modules listed in devDependencies. > > The --only={prod[uction]|dev[elopment]} argument will cause either only devDependencies or only non-devDependencies to be installed regardless of the NODE_ENV.

Have you tried the following?

npm install --only=dev

Solution 2 - node.js

npm i -D

An optional short version.

Solution 3 - node.js

npm install thePackageName --save-dev

This works fine for me.

Solution 4 - node.js

As of npm version 7.10.0 you can omit certain types of dependencies, however you cannot omit "the" dependencies (production) anymore. That's why there is no solution for this problem anymore.

Solution 5 - node.js

The --only=dev option is no longer supported. To do the dev dependency install run npm install --production=false

Solution 6 - node.js

Running npm install, It will install all dependencies under devDependencies` or dependencies.

For installing and save packages as dev dependencies in package.json, npm install package_name --save-dev or pass option -D

For installing all packages under devDependencies, npm install --only=dev

For installing and save packages as prod or only dependencies in package.json, npm install package_name --save-prod or pass option -P or npm install package_name

For installing all packages under dependencies or Prod dependencies, set Environment variable NODE_ENV=production or pass it with the command NODE_ENV=production npm install or npm install --only=prod

Instead of using install in npm command like npm install you can just use i like npm i, short of install.

Reference

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNesan RajendranView Question on Stackoverflow
Solution 1 - node.jsAhmed farag mostafaView Answer on Stackoverflow
Solution 2 - node.jsRoger MuscitoView Answer on Stackoverflow
Solution 3 - node.jsJeffView Answer on Stackoverflow
Solution 4 - node.jsMichael KView Answer on Stackoverflow
Solution 5 - node.jsdevspeterView Answer on Stackoverflow
Solution 6 - node.jsPiyush SonigraView Answer on Stackoverflow