Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

node and npm vanish or are not found when nvm is installed

Errornpm or node not found after nvm use or in a new terminal

Short 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.

  • Applies to nvm 0.41, Node.js 22, bash 5, zsh 5.9, Ubuntu 24.04, macOS 15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 current
  2. 2Match what you see, then jump to its fix

    1. 1Works after nvm use 22, gone in the next terminalNo default alias, so new shells start with nothing usefulFix
    2. 2nvm current reports system in new shellsA system Node on PATH is set after the nvm linesFix
    3. 3npm or node not found right after switching versions, though nvm lists themA prefix setting in npm config fights with nvmFix
    4. 4npm 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

What does the error look like?

  • npm or node not found after nvm use or in a new terminal
  • nvm 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 ls

2Fix

Install a version if needed and set it as default.

nvm install --lts
nvm alias default --lts node

3Verify

Open a new terminal and ask for the versions.

node -v
npm -v

Confirmed by github.com

Did this fix it?

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/null

2Fix

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 current

Confirmed by github.com

Did this fix it?

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 --lts

3Verify

Ask which npm is active.

nvm which current

Confirmed by github.com

Did this fix it?

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 --lts

3Verify

Run the script as the automation would.

npm -v

Confirmed by github.com

Did this fix it?

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

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.