Skip to content

Dependencies and ports in every worktree.

A new worktree is a clean checkout: no installed packages, no .env, and a dev server that wants the same port as every other worktree. A few lines fix all three.

Applies to Claude Code, Codex, OpenCode, Cursor Agent, Gemini CLI and any CLI agent6 min read

Dependencies

Each worktree needs its own node_modules, because packages resolve relative to the folder. With pnpm the cost is small: its store links the same files into every worktree instead of copying them.

Terminal
cd ~/code/payments-api-ledger-export
pnpm install --frozen-lockfile

Untracked files

Files git does not track, such as .env or local certificates, are not in a new worktree. Copy them from the main checkout:

Terminal
cp ../payments-api/.env .env

One port per worktree

Two dev servers cannot share a port. Give each worktree its own:

Vite takes the port on the command line:

Terminal
pnpm dev --port 5174

Next.js and many Node servers read PORT:

Terminal
PORT=3001 pnpm dev

Vite moves to the next free port by itself unless strictPort is set, and prints the address it chose. Read it from the output rather than assuming.

Shared services

Worktrees that share a database or a queue share its data too. Before you run migrations or workers, name a database or schema and a queue of its own in each worktree’s .env.

With Dockazor

The project’s .dockazor/config.json holds the commands, tracked with the project so everyone gets the same setup:

.dockazor/config.json
{
  "base": "main",
  "setup": "pnpm install && cp ../../../.env .env",
  "run": { "command": "pnpm dev" },
  "publish": { "checks": "pnpm test" }
}
  • setup runs in each new worktree before its first chat. With worktrees in their default folder, ../../../ is the project. If it fails, fix it and choose Run Setup in the Terminal tab.
  • run is the server command each worktree runs. Give each its own port as above; Dockazor reads the address the server prints, and Preview ⌘5 shows its page, from a container too.
  • Every key, and when Dockazor asks you to accept a change: the docs.