How to install packages to devDependencies using npm

Jul 20, 2021

Sometimes you want to install a dependency for your JavaScript / Node project that you only need during development or at build time, like eslint or typescript or a types package like @types/node. These packages should be installed to devDependencies because you don't need them in production.

 

The usual npm install some_package won't do the job here because it'll only install the package to dependencies (which are installed in production).

Install a package to devDependencies

npm install -D some_package
 

Here's a more verbose flag that achieves the same result:

npm install --save-dev some_package
 

Installing only your devDependencies

You can install only the devDependencies with this command:

npm install --only=dev
 

Curiously, the --only=dev flag is mentioned in the npm v6 docs but not in the v7 docs. And the npm v7 changelog doesn't mention it either (like if it had been removed).