Maaz Zindani

Loading the index…

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

npm works in your terminal but sudo cannot find it

Errorsudo: npm: command not found

Short answer

Sudo does not use your shell PATH. It replaces it with its own default, so an npm installed in your home folder, for example by nvm, is invisible to it. Check where npm lives. If it is under your home directory, stop using sudo and fix the folder permissions instead. If npm is not installed at all, install Node.js first.

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

Fix it fast

  1. 1Find out which cause you have

    Run these without sudo. The path they print tells you whether npm is in a user folder or a system folder, or is missing.

    command -v npm
    command -v node
  2. 2Match what you see, then jump to its fix

    1. 1command -v npm prints a path like /home/you/.nvm/versions/node/v22.x/bin/npmnpm lives in your home folder and sudo resets PATHFix
    2. 2command -v npm prints nothing, and plain npm also failsNode.js and npm are not installed on this machineFix
    3. 3nvm: command not found, or node is missing in new terminals onlynvm is installed but the shell never loads itFix

What does the error look like?

  • sudo: npm: command not found
  • sudo: node: command not found

Cause 1 of 3

npm lives in your home folder and sudo resets PATH

1Check

Sudo runs commands in a minimal environment and does not pass your own PATH through by default. Anything installed under your home directory, such as an nvm Node.js, is not found.

command -v npm
sudo sh -c 'echo $PATH'

2Fix

Run the command without sudo. With nvm the global folder belongs to you, so npm install -g needs no elevated rights. If a project needs root for another reason, run only that part with sudo.

npm install -g typescript

3Verify

The install finishes with no permission error, and the tool is on your PATH.

command -v tsc

Confirmed by sudo.ws

Did this fix it?

Cause 2 of 3

Node.js and npm are not installed on this machine

1Check

If neither node nor npm answers without sudo, there is nothing for sudo to find.

node --version
npm --version

2Fix

Install Node.js with a version manager. npm's own guide recommends one, and says plain installers can cause permission errors for global packages. For nvm, run its install script from the nvm README, open a new terminal, then install the LTS release.

nvm install --lts
nvm use --lts

3Verify

Both commands print a version.

node --version
npm --version

Confirmed by docs.npmjs.com

Did this fix it?

Cause 3 of 3

nvm is installed but the shell never loads it

1Check

nvm is a shell function loaded from your profile file. A new macOS zsh setup may have no ~/.zshrc, so the lines the installer adds never run.

command -v nvm
ls -a ~ | grep -E "zshrc|bashrc|bash_profile"

2Fix

Create the profile file if needed, add the loader lines from the nvm README, and open a new terminal.

touch ~/.zshrc
echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.zshrc
echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> ~/.zshrc

3Verify

In a new terminal, command -v nvm prints nvm.

command -v nvm

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Install Node.js with a version manager so global installs never need sudo.
  • Treat sudo npm as a smell: it creates root-owned files in your home folder.
  • Keep one profile file that loads nvm, so every new terminal gets the same node.

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.