Node installed with nvm is missing when you use sudo or log in as root
sudo: node: command not foundShort answer
nvm is a shell function loaded from your own profile and its files sit in your own home folder. sudo resets PATH and gives the command root's HOME, so the nvm install is not found. Run the node binary by its full path, or load nvm.sh from your user's NVM_DIR inside the root shell.
Fix it fast
1Find out which cause you have
Compare what your own shell resolves with what root resolves. If the first command prints a path under .nvm and the second fails, the problem is the sudo environment, not nvm itself.
nvm which current sudo sh -c 'echo "HOME=$HOME"; command -v node'2Match what you see, then jump to its fix
- 1
sudo: node: command not found, while node works without sudosudo does not use your PATHFix - 2
nvm: command not found inside sudo -i or su -root has its own home folder, so root has no nvmFix - 3
node works in your terminal but not from cron, a deploy script or ssh host commandA non-interactive shell never loads nvmFix
- 1
What does the error look like?
sudo: node: command not foundnvm: command not found
Cause 1 of 3
sudo does not use your PATH
1Check
sudo starts the command in a minimal environment and sets PATH to a default (or to the secure_path value when one is configured). The folder under ~/.nvm that holds node is not in it.
command -v node
sudo sh -c 'echo $PATH'2Fix
Give sudo the full path of the node binary that nvm reports for the active version.
sudo "$(nvm which current)" app.js3Verify
The script starts under sudo without the not-found message.
sudo "$(nvm which current)" --versionConfirmed by github.com
Cause 2 of 3
root has its own home folder, so root has no nvm
1Check
nvm is a sourced shell function, not an executable. It is loaded from NVM_DIR, which defaults to ~/.nvm. For root, sudo sets HOME to the target user's home (/root), so your ~/.nvm is not the one root looks at.
sudo -i
echo "$HOME"
ls -d "$HOME/.nvm"2Fix
Inside the root shell, point NVM_DIR at the folder of the user that installed nvm and source nvm.sh from there. Replace deploy with that user's name.
export NVM_DIR=/home/deploy/.nvm
. "$NVM_DIR/nvm.sh"
nvm use default3Verify
nvm is now a known command in the root shell.
nvm currentConfirmed by github.com
Cause 3 of 3
A non-interactive shell never loads nvm
1Check
nvm is activated by lines in your shell profile. A non-interactive bash does not read them, so nvm and node are both missing.
bash -c 'command -v nvm || echo "nvm not loaded"'2Fix
Either source nvm.sh at the top of the script, or set BASH_ENV (which bash reads when invoked non-interactively) to a file that does it. For predictable scripts, pass an explicit Node version.
#!/usr/bin/env bash
export NVM_DIR="$HOME/.nvm"
. "$NVM_DIR/nvm.sh"
nvm use 22
node app.js3Verify
Run the script the way cron or the deploy tool runs it.
env -i HOME="$HOME" bash ./run.shConfirmed by github.com
How do I stop it coming back?
- Avoid sudo for npm install -g when nvm is in use; the nvm documentation says it is not needed there.
- For services, put the absolute node path from nvm which in the unit or process manager instead of relying on PATH.
- Pin an explicit Node version in scripts that run unattended.
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
Application Load Balancer returns 502 for a Node.js target
HTTPCode_ELB_502_Count
An ALB returns 502 when the target gives it something it cannot use: a refused or reset connection, a connection closed mid-request, a bad response, or a header over 32 K.
4 causes, each with a fixBack end
corepack enable fails: permission denied while creating the pnpm link
Internal Error: EACCES: permission denied, symlink '../lib/node_modules/corepack/dist/pnpm.js' -> '/usr/local/bin/pnpm'
corepack enable writes small link files for yarn and pnpm next to your node binary.
2 causes, each with a fixBack end
Create React App says Node is too old, and npm will not run on the new one
npm does not support Node.js v10.19.0
The shell is running an old Node.js that is below the version Create React App needs.
3 causes, each with a fixBack end
firebase deploy cannot load your functions: user code failed to load
Functions codebase could not be analyzed successfully. It may have a syntax or runtime error
The Firebase CLI loads your functions code on your machine before it deploys, to learn which functions exist.
4 causes, each with a fixBack end
firebase deploy stops because the predeploy lint step failed
Error: There was an error deploying functions
The predeploy hook in firebase.json runs npm run lint before every deploy, and a failing hook cancels the deploy.
3 causes, each with a fixBack end
fluent-ffmpeg cannot find the ffmpeg or ffprobe binary
Error: Cannot find ffprobe
fluent-ffmpeg does not bundle ffmpeg.
4 causes, each with a fix