Skip to content
WorktreeWise
#node-modules#javascript#npm#git-worktree

node_modules in worktrees

W

WorktreeWise Engineering Team

Updated Sep 20266 min read

TL;DR: The 30-Second Summary

Should each Git worktree have its own `node_modules` or should they be shared? Individual `node_modules` guarantees 100% dependency safety when branches diverge, while modern package managers (pnpm, yarn) use content-addressable storage to prevent disk bloat.

The Real-Life Scenario

Branch A upgrades React from 18 to 19, while Branch B is a maintenance patch on React 18. Attempting to share a single `node_modules` folder causes compilation errors and runtime hook crashes in both branches.

`node_modules` directories in modern JavaScript/TypeScript projects can easily consume 500MB to 2GB of disk space. When working with multiple Git worktrees, developers must choose between complete directory isolation and storage efficiency.

The Problem with Sharing Mutable node_modules

If two worktrees share a single `node_modules` folder via symlinks, running `npm install` in Branch A mutates the package tree for Branch B. Divergent lockfiles will break builds.

What you see in the terminal:

terminal output
Error: Cannot find module 'react/jsx-runtime'
Invalid hook call. Hooks can only be called inside the body of a function component.
🔍

Under the Hood: Git Plumbing & Architecture

Symlinked `node_modules` fail whenever `package.json` or `package-lock.json` differ between branches. True isolation requires separate `node_modules` directories, backed by a global hardlink store (pnpm).

Quick Command Recipes

Copy and adapt these commands directly in your terminal:

Clean install in new worktree

Installs dependencies exactly matching the branch's package-lock.json.

bash
cd ../feature-worktree && npm ci

Check disk usage of node_modules

Compares disk consumption across all worktrees.

bash
du -sh ../*/node_modules
TUTORIAL

Step-by-Step Practical Walkthrough

Follow these verified steps to safely resolve the issue and guarantee that your filesystem and Git references are in sync.

1

Create worktree

Spawn the clean directory.

terminal
git worktree add ../feat-ui -b feat/ui main
2

Run npm ci

Install branch-accurate dependencies.

terminal
cd ../feat-ui && npm ci
3

Verify dependency isolation

Confirm the installed version matches the branch requirements.

terminal
npm list react

Edge Cases & Advanced Scenarios

Monorepo hoisting

In Turborepo/Nx monorepos, root `node_modules` and package `node_modules` must both be isolated per worktree.

⚠️

Common Mistakes to Avoid

❌ Mistake: Symlinking `node_modules` across branches with different dependency versions

Why it causes trouble: Bizarre runtime errors and broken build outputs that disappear on clean rebuilds.

What to do instead: Keep `node_modules` separate, or use pnpm.

Verification Checklist

  • Each worktree contains its own `node_modules` folder
  • Dependency upgrades in one worktree do not break sibling worktrees
💡

Senior Engineering Tips

  • Switching from npm to pnpm reduces multi-worktree disk overhead by 90% through automatic global content-addressable storage.

Key Takeaways

  • 01.Never share mutable `node_modules` across branches with divergent dependencies.
  • 02.Use `npm ci` for fast, reproducible installs.
  • 03.Adopt pnpm for zero-cost per-worktree dependencies.
TOOL

How WorktreeWise Solves This Visually

WorktreeWise eliminates the manual friction and mental overhead of CLI flags. It displays real-time branch states, uncommitted modifications, active terminals, and lock statuses across all worktrees on a single visual dashboard.

WorktreeWise interface options for managing node_modules across worktrees
WorktreeWise interface options for managing node_modules across worktrees
W

WorktreeWise Engineering Publication

Written and curated by the core WorktreeWise team. We build developer tools that turn Git worktrees, workflows, and parallel AI coding agents into second nature.

Related Isolation Guides

View all isolation