Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reduce node_modules in npm

when i start a project i always use npm and every start i will use

npm install

and will make node_modules directory inside.

When finished making the project I was surprised by the size of the file is almost 200Mb. Imagine if creating more projects. My disk capacity will be burdened.

project capacity

Is there any way to prevent/reduce the size on my project folder. For example make one file node_modules to be used repeatedly?

like image 374
laptopapik Avatar asked May 07 '18 03:05

laptopapik


People also ask

Why is my node_modules so big?

Originally Answered: Why are node_modules so large? The module structure used to be completely nested, meaning multiple versions of the same modules could be nested within each other. This is no longer the case, so module sizes are not as big as they used to be.

How do I remove unnecessary node modules?

You can use npm-prune to remove extraneous packages. Extraneous packages are packages that are not listed on the parent package's dependencies list. If the --production flag is specified or the NODE_ENV environment variable is set to production, this command will remove the packages specified in your devDependencies.

What happens if I delete node_modules?

Anybody can suggest if I delete node_modules folder and after re-creating it, will I get all the already installed packages back in the same way they were just before deleting? YES, you will get back all the packages listed in your package. json file.

Can I delete node_modules and reinstall?

You could remove your node_modules/ folder and then reinstall the dependencies from package. json. This would erase all installed packages in the current folder and only install the dependencies from package.


1 Answers

Using --save and --save-dev when adding dependencies will make your project more size-aware. For development size is "fine" but for production, npm will only install what ever is in dependencies of your package.json thus decreasing your node_modules size (in production).

Otherwise npm prune is a great way to clean up unused modules further decreasing the size of your project. Other than that being aware of which modules you install will be the only sure-fire way of decreasing project size.

like image 154
Henrik Andersson Avatar answered Oct 01 '22 04:10

Henrik Andersson