Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does installing one npm package add many subfolders in node_modules?

I've updated node / npm which I use with my grunt projects.

However when I now want to add a package to a project myproject/ (say npm install grunt-favicon) it seems to add hundreds of folders to myproject/node_modules/ whereas it only used to add one - normally prefixed with grunt like node_modules/grunt-favicon.

My knowledge of these things is basic but these seem to be required dependencies, and even some of these items have even more dependencies.

Should it not be adding these dependencies somewhere else? If so how to i correct this?

I use npm 3.3.5, node 4.1.1, grunt-cli v0.1.13 and grunt v0.4.5.

And this screenshot shows ~/.npm on the left then ~/myproject/node_modules on the right

~/.npm on the left then

like image 423
v3nt Avatar asked Oct 05 '15 14:10

v3nt


People also ask

Does npm install create a node_modules folder?

npm install doesn't create node_modules directory.

Why is node modules folder so big?

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. However; lots of people still ship a tremendous amount of “fluff” with their modul...

Can I delete my node_modules folder?

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.

Does npm install multiple versions of same package?

With npm or yarn, you can install a package under a custom alias. This enables you to install multiple versions of a package in the same project. Read the documentation on aliasing with npm here and yarn here.


1 Answers

This is correct, don't worry. You're still installing the packages locally, just the directory structure looks a bit different. It is a behaviour that changed in npm v3.0.0: now all the dependencies' dependencies are installed directly in the node_modules folder (as far as this doesn't cause version conflicts). This greatly reduces the file tree size.

From npm 3.0.0 release notes:

Flat, flat, flat!

Your dependencies will now be installed maximally flat. Insofar as is possible, all of your dependencies, and their dependencies, and THEIR dependencies will be installed in your project's node_modules folder with no nesting. You'll only see modules nested underneath one another when two (or more) modules have conflicting dependencies.

like image 63
mik01aj Avatar answered Sep 27 '22 17:09

mik01aj