npm works in your terminal but sudo cannot find it
sudo: npm: command not foundShort 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.
Fix it fast
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 node2Match what you see, then jump to its fix
- 1
command -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
command -v npm prints nothing, and plain npm also failsNode.js and npm are not installed on this machineFix - 3
nvm: command not found, or node is missing in new terminals onlynvm is installed but the shell never loads itFix
- 1
What does the error look like?
sudo: npm: command not foundsudo: 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 typescript3Verify
The install finishes with no permission error, and the tool is on your PATH.
command -v tscConfirmed by sudo.ws
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 --version2Fix
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 --lts3Verify
Both commands print a version.
node --version
npm --versionConfirmed by docs.npmjs.com
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"' >> ~/.zshrc3Verify
In a new terminal, command -v nvm prints nvm.
command -v nvmConfirmed by github.com
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
Back end
npm install -g fails with EACCES on /usr/local/lib/node_modules
npm error syscall access
EACCES means npm tried to write global packages into a folder your user does not own, usually /usr/local/lib/node_modules.
2 causes, each with a fixBack 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 fix