163

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. :(

Nesan Rajendran
  • 1,653
  • 2
  • 12
  • 7

7 Answers7

199

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
Community
  • 1
  • 1
Ahmed farag mostafa
  • 2,802
  • 2
  • 14
  • 33
  • 12
    Just now I found out that my npm version was 2.x . I upgraded it to v3.x by running the command `npm install npm -g` and **--only=dev** option worked like a charm. Thanks for the response. – Nesan Rajendran May 03 '16 at 08:52
  • 1
    The version issue mentioned by @NesanJoseph was the problem in my case too. In the older version of npm, `npm install --prod` used to install items from `dependencies` folder. Both `npm install` and `npm install --dev` used to install from *both* the `dependencies` and `devDependencies` folders! – Shiyaz Jul 13 '17 at 13:26
  • I'm getting the same issue @ricka mentions using nvm to run node v 10.9.0 and npm v 6.4.1 (both latest versions as of 10/18) and also using --only=dev is still installing app dependencies as well. Basically npm install flags are 100% useless at this point. – Henry Oct 13 '18 at 20:59
  • 2
    Not working for me (node v12.19.0, npm v6.14.8). Perhaps it has something to do with this issue open since April 2016: https://github.com/npm/npm/issues/12184 – Fappaz Oct 19 '20 at 20:39
  • 15
    It is wort noting that `--only=dev` is deprecated and will no longer work. I do not believe there is currently a way to install only the dev dependencies. – Michael Murphy Jul 25 '22 at 12:11
  • 2
    @MichaelMurphy UPVOTE HIM ! After npm 8, there is no way to only install devDependencies – julio Dec 08 '22 at 15:32
72
npm i -D

An optional short version.

Soviut
  • 88,194
  • 49
  • 192
  • 260
Roger Muscito
  • 869
  • 7
  • 6
  • 6
    YES, I had done `npm install -D` and it worked. so I was surprised and googled this, after many answer I see your answer :) – its4zahoor Dec 09 '19 at 18:31
  • 1
    The command "npm install -D" did not work for me, the node_modules is the same size as when i run "npm i" – Cristian Contreras Mar 03 '22 at 17:10
  • 1
    I am not certain that `npm i -D` is doing what you expect. I can't see any reference to this in the documentation. I am assuming that the -D flag is being ignored and a normal `npm i` is being performed here. https://docs.npmjs.com/cli/v8/commands/npm-install – Michael Murphy Jul 25 '22 at 12:08
39
npm install thePackageName --save-dev

This works fine for me.

mostafazh
  • 4,144
  • 1
  • 20
  • 26
Jeff
  • 787
  • 5
  • 4
19

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.

Michael K
  • 1,070
  • 1
  • 10
  • 25
  • 5
    I wonder why that line was arbitrarily drawn. Dependencies are bulky and with things running in containers now, it's often unnecessary to have all the dependencies installed for development. – Willem Ellis Sep 14 '21 at 17:14
9

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

devspeter
  • 516
  • 4
  • 7
  • 7
    Correct: --onyl=dev has been removed. Wrong: --production=false is not a replacement for the initial question: install ONLY devDependencies (= do NOT install dependencies) – Michael K Apr 26 '21 at 14:37
  • 1
    `npm install --production=false` will install all dependencies including normal and dev as this is what you would want when not in production (when you are in development). – Michael Murphy Jul 25 '22 at 12:10
5

In the latest version of npm there is no way to install just the dev dependencies. But there is a workaround which you can do.

You can create another package_dev.json file where you can put only devDependencies and keep the dependencies empty.

Than to install just the dev dependencies you can execute the below script

cp package.json temp.json && \
cp package_dev.json package.json && \
npm install && \
rm -rf package.json && \
cp temp.json package.json && \
rm -rf temp.json

I've got the similar requirement where I need to create a gitHub action and just wanted to install dev dependencies. The above workaround worked like charm for me.

The only Con of the approach is that you need to take care of updating the package_dev.json every time when there is an update in package.json file.

Mitesh Agrawal
  • 238
  • 3
  • 9
3

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

Piyush Sonigra
  • 1,423
  • 11
  • 10
  • This does not answer the OP question: "install ONLY the "devDependencies" listed in my package.json file" – Michael K Apr 26 '21 at 14:47