Nesan Rajendran
Nesan Rajendran

Reputation: 1863

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

Upvotes: 184

Views: 217060

Answers (7)

Mitesh Agrawal
Mitesh Agrawal

Reputation: 288

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.

Upvotes: 5

Michael K
Michael K

Reputation: 1180

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.

Upvotes: 22

devspeter
devspeter

Reputation: 552

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

Upvotes: 9

Roger Muscito
Roger Muscito

Reputation: 1147

npm i -D

An optional short version.

Upvotes: 99

Piyush Sonigra
Piyush Sonigra

Reputation: 1648

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

Upvotes: 3

Ahmed farag mostafa
Ahmed farag mostafa

Reputation: 2964

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

Upvotes: 210

Jeff
Jeff

Reputation: 951

npm install thePackageName --save-dev

This works fine for me.

Upvotes: 53

Related Questions