How to reduce size of node_modules folder?

Better Stack Team
Updated on March 11, 2024

Reducing the size of the node_modules folder in a Node.js project can be important, especially when deploying applications or managing version control. Here are several strategies you can use to minimize the size of your node_modules folder:

Use npm and Node.js LTS

Ensure that you are using a recent version of npm and Node.js. Newer versions of npm have improvements in dependency resolution and performance.

Use .npmignore or .gitignore

Create a .npmignore file to specify files and folders that should not be included when your package is installed.

# .npmignore

If you're using Git, make sure to add these entries to your .gitignore as well.

Prune Unnecessary Packages

Use the npm prune command to remove extraneous packages that are not listed as dependencies in your package.json.

npm prune

Use npm ci for Continuous Integration

For continuous integration, consider using npm ci instead of npm install. The npm ci command is designed for installing dependencies in a CI environment and can be faster.

Remove Unnecessary DevDependencies

If you are deploying your application and not actively developing, consider not including development dependencies. Install your dependencies with the -production flag.

npm install --production

Use Yarn and Yarn Workspaces

Consider using Yarn, an alternative package manager, which often performs better in terms of speed and disk space usage.

Yarn Workspaces can also help manage dependencies more efficiently.

Bundle Dependencies with Webpack or Rollup

If applicable, consider bundling your application using tools like Webpack or Rollup. This can result in a smaller bundle size, including dependencies.

Use a Docker Multi-Stage Build

If you are containerizing your application with Docker, use a multi-stage build. In the first stage, install dependencies and build your application, and in the second stage, only copy the necessary artifacts.

Check for Duplicate Packages

Use tools like npm-dedupe or yarn-deduplicate to identify and eliminate duplicate packages.

Use a Package Size Analyzer

Tools like Bundlephobia or source-map-explorer can help you analyze the size of your dependencies.

Remember that some strategies may be more suitable for specific use cases or project configurations. Experiment with different approaches based on your project's requirements and constraints.

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.

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

See the full list of amazing projects on github