npm workspaces
npm workspaces automatically symlink the node_modules directory for all child projects (workspaces) that have a packages.json.
Here is an example project structure
PROJECT /
node_modules
packages /
client /
package.json
server /
package.json
wt-main/
client/
server/
package.json
All of the workspace packages have their own package.json but will share the top-level node_modules.
Run npm install from the root to resolve dependencies for all workspaces. npm install dependencies for specific workspaces with npm install package -w workspace. For example, I can stall foo for my wt-main workspace by doing npm install foo -w packages/wt-main.
Workspace configuration in package.json
"workspaces": [
"packages/*"
]
The above configuration assumes all the worktrees are in packages/. The worktree name cannot be the same as the branch. Hence why the main branch worktree is name wt-main instead of just main.
Create a worktree
Create new worktree for main branch
git worktree add packages/wt-main
Switch to the worktree
cd packages/wt-main
Start the NPM project like normal
npm run start (or whatever is used)
There will be a noticeable lack of node_modules anymore in the workspaces / worktrees but the project still works!
.gitignore
Finally, ignore any packages that are actually worktrees. The pattern here is to start worktree directory names with with wt-.
packages/wt-*