node_modules Structure & Hoisting
Understand why npm flattens dependencies, the danger of phantom dependencies, and how pnpm fixes it with symlinks.
The problem with nested dependencies
In early versions of npm, node_modules was strictly nested. If your app installed express, and express needed accepts, npm placed accepts inside express's own node_modules directory.
This created massive duplication (downloading the same package 50 times) and caused Windows to frequently crash because the file paths exceeded the 260-character limit.
The Flattening (Hoisting) Solution
To fix this, npm 3 introduced hoisting. When you install a dependency, npm tries to 'flatten' the tree by lifting all sub-dependencies up to the root node_modules folder.
This solves the duplication and path length issues, but creates a dangerous new problem: Phantom Dependencies.
Phantom Dependencies
Because sub-dependencies like lodash are hoisted to the root node_modules, your code can accidentally import 'lodash' even though you never explicitly listed it in your package.json.
If the top-level package later drops its dependency on lodash, it gets removed from the root, and your app suddenly crashes in production with a Module not found error.
Inspect a dependency layout
Compare a sample hoisted layout with a symlinked layout and inspect which imports are available.
// app/package.json
{
"dependencies": {
"toolkit": "1.0.0"
}
}
// toolkit depends on helper
// app/index.cjs
require("helper");
Choose Check example import.Illustrative hoisted root
- node_modules/toolkit
- node_modules/helper
The graph shows declared relationships, not just physical folder proximity.
Undeclared imports can accidentally work with hoisting. Declaring a direct dependency makes the app's requirement explicit. These simplified layouts illustrate Node resolution; npm/pnpm configuration, peers and hoisting options can change the actual structure. Local simulation.
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
Remember this
- npm flattens
node_modulesto save space and avoid long file paths. - Hoisting creates 'Phantom Dependencies', allowing you to import packages you didn't explicitly install.
- pnpm's usual isolated layout reduces accidental undeclared imports; hoisting and linker configuration can change what is accessible.
Done with this concept?
Mark it complete to track your progress. No login needed.