Why Node Modules Weighs More Than the Universe

Why does node_modules weigh more than the entire universe? 🤔 If you have ever checked the size of node_modules and wondered why it can reach hundreds of megabytes or even gigabytes while your source code is tiny, you are not alone. This is one of the most common realities in the JavaScript ecosystem. Here is why it happens, explained with simple analogies: 1️⃣ The Matryoshka Doll of Transitive Dependencies 🔹 Imagine buying only flour for a cake, but the package arrives with an entire mini-bakery: sugar, yeast, and recipes for ten other items, each containing their own ingredients. 🔹 That is how npm works. One library pulls dozens or hundreds of transitive dependencies, creating a deeply nested tree inside node_modules. 2️⃣ JavaScript Ships the Full Recipe Book, Not the Finished Cake 🔹 Unlike compiled languages such as Go or Rust that deliver compact binaries, Node.js requires the original source code for Just-In-Time compilation. 🔹 Most packages therefore include not only JavaScript files, but also TypeScript definitions, tests, documentation, polyfills, multiple module formats, and examples. It is like receiving the complete cookbook and all utensils with every single ingredient. 3️⃣ The Duplicate Editions Problem 🔹 Different parts of your project may require slightly different versions of the same library. Semantic versioning rules force npm to install separate copies in different folders, leading to significant duplication. 4️⃣ The Culture of Micro-Packages 🔹 The ecosystem favors many small, single-purpose packages. This modularity multiplies the total number of entries in node_modules. In summary, the large size of node_modules results from deliberate design choices: complete dependency isolation, delivery of full source code, and a preference for tiny reusable modules. The good news is that tools like pnpm use a global store to deduplicate packages, often reducing size by 70 to 90 percent. You can also review your package.json and prune unnecessary dependencies. Have you measured your node_modules recently? What is the largest size you have seen? Share your experiences below. #NodeJS #TypeScript #JavaScript #WebDevelopment #SoftwareEngineering

  • diagram

To view or add a comment, sign in

Explore content categories