How Do You Prevent Install of “devDependencies” npm Modules for node.js (package.json)?

Better Stack Team
Updated on April 4, 2024

When using npm install in Node.js, by default, both regular dependencies and devDependencies specified in the package.json file are installed. If you want to install only production dependencies (excluding devDependencies), you can use the --production flag.

Here's how you can do it:

 
npm install --production

This command installs only the dependencies listed in the "dependencies" section of your package.json file, skipping the packages listed in the "devDependencies" section.

If you want to make this behavior the default for your project, you can set an environment variable:

 
export NODE_ENV=production
npm install

Or, on Windows:

 
set NODE_ENV=production
npm install

Setting NODE_ENV to "production" automatically skips installing devDependencies. This is because npm will assume you are deploying your application in production when NODE_ENV is set to "production".

Keep in mind that during development, you might still need to install devDependencies. If you switch back to development mode, ensure to unset or change NODE_ENV accordingly.

It's also worth noting that some tools and scripts may rely on packages listed in devDependencies even during development, so be cautious about excluding them in certain situations.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github