# How to reduce size of node_modules folder?

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
tests/
documentation/
```

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`.

```bash
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.

```bash
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](https://bundlephobia.com/) or [source-map-explorer](https://github.com/danvk/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.