Infinite | Squares

Art of code and more

Flatten Node_modules

If you worked with Node.js, you might start notice youd node_modules started to simply nested into the abyss and the path is getting longer and longer. Consider this :


Yes, it is a headache to see deeply nested node_modules. You got more headache when you see that there are some duplicated node_modules pop out elsewhere. Well, the headache part is subjective my opinion.

I think npm is designed to work in fire and forget style, when you install a module, you don’t have to think dependencies hell where some modules require one module with different version number. If you install the latest, then other module simply break. You can’t control that other modules author might choose to ignore if their modules dependency is updated with breaking changes. The solution is thet each module has its own node_modules and each module in node_modules has its own node_modules and so on.

A while ago, I wrote a Node.js module named flatten-packages that will flatten your node_modules into one level and removing all duplicates by aggressiely delete old version when new one is found. You can find the project on github. There are things to consider when using flatten-packages:

  • Your node_modules folder might explode with numbers of previously nested node_modules.
  • Version changes might break some of your dependency
  • You might be confused when trying to relate your package.json with exploded node_modules

With flatten-packages -i command, you can get summary of your node_modules hierarcy.

Sample flatten-packages -i output

Flatten small

From 17 modules into 73 modules. I can live with that

Now consider this

Flatten big

8 layers deep of node_modules with 514 duplicates! If I flatten it, my node_modules folder with explode from 33 modules into 271 modules!

As I have said before, with npm you shouldn’t worry about those deeply nested node_modules which cause Windows issue when filename has more than 260 characters and modules duplicates (514 in above case) that eat up your disk space (disk is cheap now). It’s just unlucky for me to work on Windows with ony 10 GB to spare. That what causes my headache.