185

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

3
  • 1
    as today - npm WARN install Usage of the --dev option is deprecated. Use --only=dev instead. Commented Jun 16, 2020 at 9:15
  • 2
    The latest version of NPM (v7) supports --production=false instead of the above. Commented Mar 25, 2021 at 1:20
  • 3
    --production=false does not solve the OP question: it installs dependencies and devDependencies, not ONLY devDependencies. Commented Apr 26, 2021 at 14:49

7 Answers 7

212

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
Sign up to request clarification or add additional context in comments.

8 Comments

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.
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: github.com/npm/npm/issues/12184
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.
@MichaelMurphy UPVOTE HIM ! After npm 8, there is no way to only install devDependencies
I'm here because none of the long options worked after switching to npm 9, but -D does still work, and it does so without complaint. It appears the -D is now the only way.
|
106
npm i -D

An optional short version.

4 Comments

YES, I had done npm install -D and it worked. so I was surprised and googled this, after many answer I see your answer :)
The command "npm install -D" did not work for me, the node_modules is the same size as when i run "npm i"
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. docs.npmjs.com/cli/v8/commands/npm-install
As of npm 9, it appears this is no longer optional. It seems -D is now the only way.
52
npm install thePackageName --save-dev

This works fine for me.

10 Comments

--save-dev saves the package to the devDependencies in package.json
--save-dev is meant to flag that the installed package would be installed under the devDependencies
More like npm install thePackageName --save-dev
This will install a single package explicit name and save it to dev dependencies. The question is about how to install only devDependencies from the package.json.
Doesn't meet the criteria for the question. Your answer is to install: 1. a single package 2. the question asks how to install stuff that's already in the package.json but only in the dependencies list.
|
22

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.

2 Comments

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.
npm install --omit=peer for example.
10

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

2 Comments

Correct: --onyl=dev has been removed. Wrong: --production=false is not a replacement for the initial question: install ONLY devDependencies (= do NOT install dependencies)
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).
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.

1 Comment

This should be the answer for today
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

1 Comment

This does not answer the OP question: "install ONLY the "devDependencies" listed in my package.json file"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.