Solved: remove dev dependencies from node_modules

Node and its package manager, npm, are crucial tools for modern JavaScript application development. At the same time, managing Node packages can sometimes be a bit tedious. One specific issue developers often face is the handling of dev dependencies in the node_modules folder. These dependencies can be quite large, and they aren’t used in the production code. Thus, it is a common practice to remove these when building for production. In this article, we will delve into how to remove dev dependencies from the node_modules folder.

Understanding Dev Dependencies

Dev Dependencies VS Production Dependencies

In JavaScript, there’s a distinction between dev dependencies, which are required during development and testing, and production dependencies, which are needed to run the application.

  "name": "sample_project",
  "version": "1.0.0",
  "devDependencies": {
    "jest": "^26.6.3",
    "eslint": "^7.22.0"
  "dependencies": {
    "express": "^4.17.1",
    "mongoose": "^5.12.1"

Working with node_modules

The node_modules folder is where Node keeps all your project’s dependencies. But it can get crowded, particularly with dev dependencies which aren’t needed for the production version of your application.

Removing Dev Dependencies

npm prune –production

The easiest way to remove dev dependencies from your node_modules directory is using the npm command ‘npm prune –production’. This command removes packages not needed for production, leaving you with a slimmer node_modules.

$ npm prune --production

Understanding the npm prune command

The npm prune command removes “extraneous” packages. Extraneous packages are packages that are not listed on the parent package’s dependencies list. When the –production flag is added, the command also removes packages listed in devDependencies.

Step by Step Code Explanation

Step 1: Check your package.json file to ensure you have correctly categorized your dependencies into ‘dependencies’ and ‘devDependencies’.

Step 2: Run the ‘npm prune –production’ command. This will remove the devDependencies from your node_modules folder.

$ npm prune --production

Step 3: Now, your node_modules folder only contains the packages listed in the ‘dependencies’ in your package.json file.

This process significantly decreases the size of your node_modules folder and is ideally used before deploying your application to production.

Remember, the key to managing node_modules is understanding the difference between dev dependencies and production dependencies, and the npm commands that help us manage them. And the ‘npm prune –production’ is one such handy command that helps in optimizing the node_modules folder by removing unnecessary dev dependencies.

Note: The ‘npm prune –production’ command should be used carefully and only when you’re sure about the packages in devDependencies. This is because once pruned, these packages will need to be re-installed for development.

Related posts:

Leave a Comment