Skip to content
WorktreeWise
#npm#javascript#dependencies#git-worktree

npm with worktrees

W

WorktreeWise Engineering Team

Updated Sep 20265 min read

TL;DR: The 30-Second Summary

Optimize npm across parallel Git worktrees using `npm ci --prefer-offline`, cache sharing, and avoiding global `npm link` collisions.

The Real-Life Scenario

An engineer runs `npm install` across 3 worktrees simultaneously and encounters file lock errors and long network downloads. Switching to `npm ci --prefer-offline` cuts install times by 75%.

While npm historically duplicated packages on disk, modern versions of npm (v9/v10) include robust caching and offline resolution flags that make managing multiple worktrees significantly faster.

Slow Installs and Lockfile Mutations

Running `npm install` in a worktree often modifies `package-lock.json` unnecessarily and downloads packages over the network that are already cached locally.

What you see in the terminal:

terminal output
$ npm install
npm WARN idealTree Already up to date, but lockfile was updated
🔍

Under the Hood: Git Plumbing & Architecture

`npm ci` enforces strict adherence to `package-lock.json` and deletes any pre-existing `node_modules`, ensuring an exact, reproducible installation.

Quick Command Recipes

Copy and adapt these commands directly in your terminal:

Fast offline install

Reuses local npm cache without re-validating against registry servers.

bash
npm ci --prefer-offline

Verify npm cache integrity

Ensures the shared npm cache is healthy across all worktrees.

bash
npm cache verify
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 workspace.

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

Run npm ci

Install dependencies from cache.

terminal
cd ../feat-npm && npm ci --prefer-offline
3

Verify clean tree

Confirm `package-lock.json` was not modified.

terminal
git status

Edge Cases & Advanced Scenarios

npm link collisions

Avoid `npm link` between worktrees as global links overwrite each other. Use file paths (`npm i ../path/to/pkg`) instead.

⚠️

Common Mistakes to Avoid

❌ Mistake: Using `npm install` instead of `npm ci` in temporary worktrees

Why it causes trouble: Unintentionally mutates `package-lock.json` with minor dependency updates.

What to do instead: Always use `npm ci` in worktrees.

Verification Checklist

  • `package-lock.json` remains pristine
  • Build passes cleanly with installed dependencies
💡

Senior Engineering Tips

  • Add `npm ci --prefer-offline` to your WorktreeWise post-creation workflow hook to automate dependency installation.

Key Takeaways

  • 01.Always use `npm ci` rather than `npm install` in worktrees.
  • 02.`--prefer-offline` speeds up installs by reusing the local npm cache.
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 Workflows automating npm install commands upon worktree creation
WorktreeWise Workflows automating npm install commands upon worktree creation
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