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.