Programming Languages Bug Report

nvm use says Now using node v22 but node -v still prints v20: PATH order and .nvmrc

nvm reported the right Node version while the shell kept running a Homebrew node, .nvmrc was ignored on cd, and new terminals took a second to open. Three separate shell setup mistakes, and how we fixed each one.

Daniel Tran Daniel Tran @fun_dev · November 23, 2026 · 8 min read · 141 Views
605-nvm-node-version-not-switching-path

A new engineer joined the frontend team and could not get the dev server to start. Our repository requires Node 22, declared in .nvmrc and enforced through the engines field in package.json. They had nvm installed, ran nvm use, and nvm confidently said it worked. The next command disagreed:

$ nvm use
Found '/Users/dev/work/web/.nvmrc' with version <22>
Now using node v22.12.0 (npm v10.9.0)
$ node -v
v20.18.1
$ npm install
npm error code EBADENGINE
npm error engine Unsupported engine
npm error engine Not compatible with your version of node/npm: web@0.0.0
npm error notsup Required: {"node":">=22.12"}
npm error notsup Actual:   {"npm":"10.8.2","node":"v20.18.1"}

While pairing on it we noticed two more complaints from the same laptop: opening a new terminal tab took more than a second, and switching between repositories never changed the Node version unless they ran nvm use by hand.

Reproducing it

  1. Install Node with Homebrew (brew install node@20, linked) and nvm from its install script.

  2. In ~/.zshrc, put the nvm loader first and eval "$(/opt/homebrew/bin/brew shellenv)" after it.

  3. Open a new shell, cd into a repo with .nvmrc containing 22, run nvm use, then node -v.

What we ruled out

  • A broken nvm install of 22. nvm which 22 printed ~/.nvm/versions/node/v22.12.0/bin/node, and running that path directly printed v22.12.0.

  • A cached command path. zsh caches command locations; rehash did not change anything.

  • A project-local Node. There was no node_modules/.bin/node and no volta key in package.json.

type -a node gave the answer immediately:

$ type -a node
node is /opt/homebrew/bin/node
node is /Users/dev/.nvm/versions/node/v22.12.0/bin/node

Root cause

nvm works by putting the chosen version's bin directory on PATH. Something later in the shell had put /opt/homebrew/bin in front of it again. In this laptop's ~/.zshrc, the Homebrew shellenv line ran after the nvm loader. On top of that, a plugin hook re-ran brew shellenv on every prompt, so /opt/homebrew/bin kept winning even right after nvm use. The Homebrew node@20 that a previous tool had installed and linked was simply first.

The other two symptoms had separate causes: nvm does not switch versions on cd by itself, and loading nvm.sh normally runs nvm use default at startup, which is the slow part.

What is happening under the hood

There is no magic in version managers like nvm. A shell finds a program by walking PATH from left to right and taking the first match. nvm use 22 edits PATH in the current shell so ~/.nvm/versions/node/v22.12.0/bin comes before other Node directories it knows about. It cannot stop other code from prepending more directories afterwards, and it does not touch other shells, IDE terminals or GUI apps.

Homebrew's brew shellenv prints exports that prepend /opt/homebrew/bin and /opt/homebrew/sbin. If that line runs after nvm, or runs again from a plugin hook, Homebrew's directory ends up ahead of nvm's.

.nvmrc is only read when you run nvm use, nvm install or nvm exec without a version. The nvm README documents an optional shell hook ("deeper shell integration") for automatic switching on directory change; it is not on by default.

Environment

Component

Version / value

OS

macOS 15.x, Apple Silicon

Shell

zsh 5.9 with a plugin manager

nvm

0.40.x

Homebrew node

node@20 20.18.1, linked

Required Node

22.12.0 (.nvmrc says 22)

npm config

prefix=/Users/dev/.npm-global in ~/.npmrc

Minimal reproduction

# ~/.zshrc (broken order)
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
eval "$(/opt/homebrew/bin/brew shellenv)"

Any shell started with this file resolves node to Homebrew's copy first, regardless of nvm use.

Who is affected

Anyone with a second Node install on PATH: Homebrew, the official macOS .pkg (/usr/local/bin/node), a distro package on Linux (/usr/bin/node), or a Node bundled by another tool. Bash users see the same with ~/.bash_profile. Fish users are not covered by nvm at all, which is a common reason teams look at fnm or Volta.

The fix

1. Load Homebrew first, nvm last. Order matters, so the PATH setup for tools that ship their own Node goes before nvm:

+eval "$(/opt/homebrew/bin/brew shellenv)"
 export NVM_DIR="$HOME/.nvm"
 [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
-eval "$(/opt/homebrew/bin/brew shellenv)"

We also removed the plugin hook that re-ran brew shellenv on every prompt, and on this laptop unlinked the stray Node entirely, since nothing needed it:

brew uninstall node@20
type -a node          # only the nvm path should remain

If something genuinely depends on Homebrew's Node (some formulae do, as a dependency), leaving it installed is fine once nvm's directory comes first.

2. Clear the npm prefix conflict. The engineer's first nvm use after fixing the order printed:

nvm is not compatible with the npm config "prefix" option: currently set to "/Users/dev/.npm-global"
Run `nvm use --delete-prefix v22.12.0` to unset it.

A custom prefix makes global packages install outside nvm's per-version directory, so nvm refuses to manage it. We removed the line from ~/.npmrc and reinstalled the few global CLIs per version (npm install -g under 22).

3. Switch automatically on cd. We adopted the zsh hook from the nvm README in a slightly shortened form:

autoload -U add-zsh-hook
load-nvmrc() {
  local nvmrc_path
  nvmrc_path="$(nvm_find_nvmrc)"
  if [ -n "$nvmrc_path" ]; then
    local want
    want="$(nvm version "$(cat "$nvmrc_path")")"
    if [ "$want" = "N/A" ]; then
      nvm install
    elif [ "$want" != "$(nvm version)" ]; then
      nvm use --silent
    fi
  elif [ "$(nvm version)" != "$(nvm version default)" ]; then
    nvm use default --silent
  fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc

4. Speed up startup. Loading nvm with --no-use skips resolving the default version at startup. The hook above then picks the version per directory, and outside a project we set PATH to the default version's bin ourselves:

[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" --no-use

New tabs went from about 1.1 s to under 0.2 s on that laptop, measured with time zsh -i -c exit.

Alternatives and trade-offs

  • fnm. A single binary, works in zsh, bash, fish and PowerShell, reads .nvmrc and .node-version, and switches on cd with eval "$(fnm env --use-on-cd)". Much faster startup. Same PATH-order rule applies.

  • Volta. Pins Node in package.json (volta pin node@22) and uses shims, so the right version runs even in non-interactive shells and IDEs. Check its README for current maintenance status before adopting it for a new team.

  • mise or asdf. One tool for Node, Python, Java and more, reading .nvmrc among other files. Worth it if you already manage several runtimes.

  • Corepack and engines. Not a version manager, but engines plus engine-strict=true in the project .npmrc turns a wrong version into an immediate, clear failure, which is how we caught this.

How to test the fix

exec zsh
cd ~/work/web && node -v        # v22.12.0 without running nvm use
cd ~ && node -v                 # nvm default
type -a node | head -1          # an ~/.nvm path inside the repo
time zsh -i -c exit             # well under half a second

Confirming in production

Here "production" was the team's laptops and the dev containers. We added a predev script so the dev server refuses to start on the wrong major and says why:

{
  "scripts": {
    "predev": "node -e \"const m=+process.versions.node.split('.')[0]; if(m<22){console.error('Node '+process.version+' found, need 22. Run: nvm use'); process.exit(1)}\""
  }
}

In the following two weeks, nobody hit the EBADENGINE error again; two people saw the predev message once and fixed their shells in a minute.

Regression guard

  • engine-strict=true in the repo's .npmrc so npm install fails on a wrong version.

  • The onboarding doc shows the expected ~/.zshrc order and includes type -a node as a self-check.

  • CI reads the version from the same file (actions/setup-node with node-version-file: .nvmrc), so local and CI cannot drift.

Security implications

Global npm packages installed under a custom prefix outlived Node upgrades and were never updated; moving them under nvm's per-version directories made the stale ones visible and we removed several unused CLIs. Install nvm only from its official repository and review the install script before piping it to a shell. The automatic cd hook runs nvm install for versions in .nvmrc, which downloads from nodejs.org; acceptable for our own repositories, worth knowing when you cd into untrusted code.

Prevention checklist

  • Run type -a node before debugging any Node version problem.

  • Put brew shellenv and other PATH-prepending lines before the nvm loader.

  • Do not set npm prefix when using nvm.

  • Enable automatic .nvmrc loading, or switch to a manager that does it by default.

  • Enforce the version with engines and engine-strict so mistakes fail loudly.

References

Add a label

#nodejs #zsh #homebrew #nvm #fnm #volta
Written by
Daniel Tran @fun_dev

I have spent most of my career on the side of software that pages people at night: payment integrations, web platforms with real traffic, CI/CD pipelines, Kubernetes clusters and the databases underneath them. Somewhere along the way I started keeping a notebook of every bug that cost more than an hour — the exact error, what we ruled out, the real root cause and how we proved the fix. Hibug is where those notes live. Every post follows the same path: Investigate, Explain, Fix, Verify — with the environment spelled out, the fix tested, and the security angle checked.

Advertisement

Comments

0

No comments yet. Be the first to comment!

Sign In to post a comment.