Maaz Zindani

Loading the index…

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

Node installed with nvm is missing when you use sudo or log in as root

Errorsudo: node: command not found

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

  • Applies to nvm 0.40, Node.js 22, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

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

    1. 1sudo: node: command not found, while node works without sudosudo does not use your PATHFix
    2. 2nvm: command not found inside sudo -i or su -root has its own home folder, so root has no nvmFix
    3. 3node works in your terminal but not from cron, a deploy script or ssh host commandA non-interactive shell never loads nvmFix

What does the error look like?

  • sudo: node: command not found
  • nvm: 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.js

3Verify

The script starts under sudo without the not-found message.

sudo "$(nvm which current)" --version

Confirmed by github.com

Did this fix it?

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 default

3Verify

nvm is now a known command in the root shell.

nvm current

Confirmed by github.com

Did this fix it?

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

3Verify

Run the script the way cron or the deploy tool runs it.

env -i HOME="$HOME" bash ./run.sh

Confirmed by github.com

Did this fix it?

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

Next step

Want this handled for good?

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