node and npm vanish or are not found when nvm is installed
npm or node not found after nvm use or in a new terminalShort answer
nvm adds node and npm to PATH only for the version it has activated. A new shell uses the default alias, so set one. If nvm current says system, an older PATH entry wins. A prefix setting in ~/.npmrc breaks nvm. Non-interactive shells never load nvm unless you tell them to.
Fix it fast
1Find out which cause you have
Ask nvm what is active and where the binary comes from. Use command -v for nvm itself, because it is a shell function, not a program.
command -v nvm nvm current nvm which current2Match what you see, then jump to its fix
- 1
Works after nvm use 22, gone in the next terminalNo default alias, so new shells start with nothing usefulFix - 2
nvm current reports system in new shellsA system Node on PATH is set after the nvm linesFix - 3
npm or node not found right after switching versions, though nvm lists themA prefix setting in npm config fights with nvmFix - 4
npm works in your terminal but not in a Dockerfile RUN step or a non-interactive scriptA script, cron job or Docker build never loads nvmFix
- 1
What does the error look like?
npm or node not found after nvm use or in a new terminalnvm current prints system in a new shell
Cause 1 of 4
No default alias, so new shells start with nothing useful
1Check
New shells start with the default version. List the aliases and see whether default points to an installed version.
nvm ls2Fix
Install a version if needed and set it as default.
nvm install --lts
nvm alias default --lts node3Verify
Open a new terminal and ask for the versions.
node -v
npm -vConfirmed by github.com
Cause 2 of 4
A system Node on PATH is set after the nvm lines
1Check
Find the line in your shell profile that loads nvm and see whether a PATH change comes after it.
grep -n -E "nvm|PATH" ~/.zshrc ~/.bashrc ~/.profile 2>/dev/null2Fix
Move the nvm lines below the PATH line. To step back to the system Node temporarily, run nvm deactivate or nvm use system.
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"3Verify
Open a new terminal; nvm current shows your version, not system.
nvm currentConfirmed by github.com
Cause 3 of 4
A prefix setting in npm config fights with nvm
1Check
The nvm docs say a prefix in ~/.npmrc and the NPM_CONFIG_PREFIX and PREFIX variables are incompatible with nvm.
grep -n prefix ~/.npmrc
env | grep -i -E "^(NPM_CONFIG_PREFIX|PREFIX)="2Fix
Delete the prefix line from ~/.npmrc and unset the variables, then switch again.
unset NPM_CONFIG_PREFIX PREFIX
nvm use --lts3Verify
Ask which npm is active.
nvm which currentConfirmed by github.com
Cause 4 of 4
A script, cron job or Docker build never loads nvm
1Check
nvm loads from your shell profile, which non-interactive bash does not read.
bash -c 'command -v nvm || echo nvm not loaded'2Fix
For non-interactive bash, the nvm docs recommend setting BASH_ENV so nvm loads. Or source nvm.sh at the top of the script.
export NVM_DIR="$HOME/.nvm"
. "$NVM_DIR/nvm.sh"
nvm use --lts3Verify
Run the script as the automation would.
npm -vConfirmed by github.com
How do I stop it coming back?
- Always set a default alias right after installing nvm.
- Keep the nvm loader lines at the end of the shell profile.
- Commit an .nvmrc so everyone and every script uses the same version.
- Never set a global prefix in ~/.npmrc on a machine that uses nvm.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
Node installed with nvm is missing when you use sudo or log in as root
nvm: command not found
nvm is a shell function loaded from your own profile and its files sit in your own home folder.
3 causes, each with a fixBack end
The nest command is not found in the terminal
bash: nest: command not found
The shell cannot find a nest executable on its PATH.
3 causes, each with a fixBack end
npm install worked but the command is not found
bash: vite: command not found
npm puts a local install's command in the project's node_modules/.bin, which is not on your shell PATH, so run it with npx or from an npm script.
3 causes, each with a fixBack end
The serverless command is not found in the shell
bash: serverless: command not found
The shell can only run serverless if it is installed globally and the folder npm links global commands into is on PATH.
3 causes, each with a fixBack end
npm or a script fails in WSL with /usr/bin/env: 'bash\r'
/usr/bin/env: 'bash\r': No such file or directory
The \r in the message is a Windows line ending at the end of a script's first line, so Linux looks for a program called bash followed by a carriage return.
2 causes, each with a fixBack end
yarn command not found, or yarn global binaries not found
zsh: command not found: yarn
The shell cannot find the yarn binary or a tool installed with yarn global.
4 causes, each with a fix