14

I have a big node repo that has a node_modules folder, which is untracked (included in the .gitignore file).

I recently started learning about git worktrees and I would like to be able to add worktrees that include the node_modules folder without having to run npm install on that worktree. Is there a way to accomplish that without editing .gitignore or copying the folder manually? I just would like to have worktrees ready to go when creating them.

1
  • 1
    Short answer: no. Slightly longer: it's not a good idea in general since your added working tree on some other branch might use different versions of various npm modules. If you're sure you will be using the same ones, you can of course do the copy-manually step. I would avoid committing these files as once something is committed in Git, it is saved forever (well, by default: it's sometimes possible to get rid of these, but it can be very difficult). Commented Dec 13, 2022 at 1:56

3 Answers 3

3

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-*
Sign up to request clarification or add additional context in comments.

2 Comments

I think this will only work if you have separate git repositories for each package (which doesn't seem like a good idea), rather than an overall repository for the monorepo. Assuming 1 repo for your workspace, a separate worktree will just be the whole thing, including the parent level node_modules.
I tested this with one git repo but multiple npm projects and it worked. If configured correctly it shouldn't do that. It took me some trial and error.
0

Not sure if this is what you mean, but you can temporarily remove node_modules from .gitignore and add that line back after you've pushed the contents upstream.

1 Comment

This violates the OP's "without editing .gitignore" step, and once you've added and committed the node modules files and pushed them to GitHub, you're stuck with them forever (assuming GitHub is the upstream; GitHub never delete any commits no matter how hard you work on your end).
0

So there's not a way, that I can think of, that accomplishes what you're asking without violating at least one of your prerequisites. You will have to do one of the 3:

  1. Run npm install within your worktree.
  2. Manually copy node_modules.
  3. Temporarily remove node_modules within your .gitignore (this would run into the issue of having to remove previously tracked node_modules from git).

I would recommend the first (run npm install within your worktree), for the exact reason that @torek stated. There could be slightly different versions of dependencies within your node_modules and unless it matches 1:1 to your package_lock.json within the worktree, you're in for a world of frustration and you'll end up running npm install anyways.

The main use case I have for a worktree is because I am working on a feature branch that has a different set of node_modules (say an angular version upgrade feature branch) and I need to checkout develop or another feature branch for a code review and I don't want to have to run npm install every time I checkout to a new branch.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.