ChatGPT解决这个技术问题 Extra ChatGPT

How to install only "devDependencies" using npm

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

as today - npm WARN install Usage of the --dev option is deprecated. Use --only=dev instead.
The latest version of NPM (v7) supports --production=false instead of the above.
--production=false does not solve the OP question: it installs dependencies and devDependencies, not ONLY devDependencies.

C
Community

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

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.
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!
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.
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
S
Soviut
npm i -D

An optional short version.


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"
m
mostafazh
npm install thePackageName --save-dev

This works fine for me.


--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.
M
Michael K

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.


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.
d
devspeter

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


Correct: --onyl=dev has been removed. Wrong: --production=false is not a replacement for the initial question: install ONLY devDependencies (= do NOT install dependencies)
P
Piyush Sonigra

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


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