# node_modules Structure & Hoisting

> Understand why npm flattens dependencies, the danger of phantom dependencies, and how pnpm fixes it with symlinks.

Subject: Package Managers | Lesson 8 of 24 | 4 min read
Interactive lesson: https://codewithanimation.in/package-managers/node-modules-and-hoisting
Previous: [dependencies vs devDependencies](https://codewithanimation.in/package-managers/dependencies-vs-devdependencies.md) | Next: [The Lockfile (package-lock.json)](https://codewithanimation.in/package-managers/package-lock-json.md)

## 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.

## Tip: Symlinked strictness (pnpm)

Modern package managers like **pnpm** solve this by strictly separating the actual files in a hidden `.pnpm` global store, and using symlinks to reconstruct a strict `node_modules` folder.

This gives you the disk-space savings of hoisting, while preventing your code from accessing phantom dependencies.

## Try it: Inspect a dependency layout

Compare a sample hoisted layout with a symlinked layout and inspect which imports are available.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/package-managers/node-modules-and-hoisting

## Check yourself

### Question 1

Why can a package manager hoist a compatible shared dependency?

- A. Hoisting deletes all dependency declarations
- B. Several dependents can resolve a shared copy higher in the tree
- C. Hoisting changes every package to the newest major
- D. Hoisting compiles JavaScript into native code

Correct answer: B

Hoisting can reduce duplication when version requirements permit sharing. Conflicting requirements may still need separate copies.

- Why not A: The dependency graph still describes required relationships.
- Why not C: Version compatibility still constrains resolution.
- Why not D: It changes installation layout, not the source language.

### Question 2

Your app imports lodash only because another package hoisted it. What is the risk?

- A. The import automatically becomes a direct declaration
- B. The package is guaranteed to remain because it was once installed
- C. An upstream dependency change can make that undeclared import disappear
- D. The package must have been installed globally

Correct answer: C

This is a phantom dependency: your app relies on availability it did not declare. The incidental layout can change.

- Why not A: Importing a package does not edit the manifest.
- Why not B: A later resolved tree can remove it.
- Why not D: Hoisted packages can be local project dependencies.

### Question 3

Your app starts importing a transitive package directly. What should you do?

- A. Hard-code its current nested node_modules path
- B. Remove the app's package.json
- C. Ask every teammate to copy its folder manually
- D. Declare it as your own direct dependency

Correct answer: D

Declare packages your code directly uses and update the lockfile through the package manager. Do not rely on an incidental hoisted location.

- Why not A: That path is an installation detail that can change.
- Why not B: Removing the manifest loses dependency declarations.
- Why not C: Manual copying bypasses normal dependency resolution.

## Remember this

- npm flattens `node_modules` to 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.
