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.
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
Install Node with Homebrew (
brew install node@20, linked) and nvm from its install script.In
~/.zshrc, put the nvm loader first andeval "$(/opt/homebrew/bin/brew shellenv)"after it.Open a new shell,
cdinto a repo with.nvmrccontaining22, runnvm use, thennode -v.
What we ruled out
A broken nvm install of 22.
nvm which 22printed~/.nvm/versions/node/v22.12.0/bin/node, and running that path directly printedv22.12.0.A cached command path. zsh caches command locations;
rehashdid not change anything.A project-local Node. There was no
node_modules/.bin/nodeand novoltakey inpackage.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 |
|
Required Node | 22.12.0 ( |
npm config |
|
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
.nvmrcand.node-version, and switches oncdwitheval "$(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
.nvmrcamong other files. Worth it if you already manage several runtimes.Corepack and
engines. Not a version manager, butenginesplusengine-strict=truein the project.npmrcturns 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=truein the repo's.npmrcsonpm installfails on a wrong version.The onboarding doc shows the expected
~/.zshrcorder and includestype -a nodeas a self-check.CI reads the version from the same file (
actions/setup-nodewithnode-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 nodebefore debugging any Node version problem.Put
brew shellenvand other PATH-prepending lines before the nvm loader.Do not set npm
prefixwhen using nvm.Enable automatic
.nvmrcloading, or switch to a manager that does it by default.Enforce the version with
enginesandengine-strictso mistakes fail loudly.
References
nvm README (usage,
.nvmrc, deeper shell integration,--no-use): https://github.com/nvm-sh/nvmHomebrew
brew shellenv: https://docs.brew.sh/Manpagenpm
enginesfield: https://docs.npmjs.com/cli/configuring-npm/package-jsonnpm
engine-strictconfig: https://docs.npmjs.com/cli/using-npm/configfnm README: https://github.com/Schniz/fnm
Volta documentation: https://docs.volta.sh/
actions/setup-node
node-version-file: https://github.com/actions/setup-node
Add a label
Embed this post
<iframe src="https://hibug.me/nvm-node-version-not-switching-path" width="700" height="420" frameborder="0" loading="lazy"></iframe>
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.
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.