Fix sudo: npm: command not found on Ubuntu

How To Solve The Error Sudo Npm Command Not Found On Ubuntu 20.04LTS

The error sudo: npm: command not found means sudo cannot locate npm in its own command search path. First check whether npm is missing for every shell or only when you add sudo, then use the matching fix.

Check which command is missing

Before you change package sources, run these checks to separate a missing installation from a user-managed Node.js installation that sudo cannot see.

node --version
npm --version
sudo -n npm --version

If node and npm both fail, install Node.js and npm. If npm works without sudo but the sudo command fails, keep project installs under your user account instead of forcing sudo to use your version-manager path.

npm-4

Install Node.js and npm with Ubuntu packages

For an Ubuntu-managed installation, update the package index and install both packages. This gives the system user and sudo the same executable locations.

sudo apt update
sudo apt install nodejs npm

Confirm both commands after apt finishes. If you need a supported Node.js release for an application, use the package source that application documents. The Node.js on Linux installation guide covers broader distribution choices, and the Ubuntu Node.js guide focuses on version selection.

When npm works without sudo

Tools such as nvm install Node.js under your home directory. Your shell receives that directory through PATH, while sudo starts with a restricted path and cannot resolve npm.

Install project dependencies without sudo. If a global tool belongs to your development account, configure its prefix under your home directory or use the version manager that installed Node.js. Root-owned node_modules folders cause permission failures later.

npm install
npm install --global <package-name>

This distinction matters when you install tools such as Pyright with npm or set up Yarn on Ubuntu. Run the package manager through the same user and Node.js installation that owns the project.

Verify PATH before reinstalling

When npm works in your terminal but sudo cannot find it, inspect the resolved executable and the paths sudo receives. This shows whether the problem is installation or environment separation.

command -v npm
printf "%s\n" "$PATH"
sudo -n sh -c "command -v npm; printf '%s\n' \"$PATH\""

Do not copy your full user PATH into sudo configuration as a quick fix. It can make root run a user-controlled executable. Install Node.js system-wide when a system service needs it, then keep routine application commands under the project user.

A quick verification checklist

Verify npm under the account that will run your application, because a version number from one account does not establish availability for another account or for a service.

  1. Run node –version and npm –version without sudo.
  2. Run sudo -n npm –version only when the task truly requires the root environment.
  3. Use npm install without sudo inside project directories.
  4. Check the Node.js version required by the application before installing global tooling.

The gtop installation walkthrough gives you a concrete npm-based command-line example, and the Mockoon installation guide covers a development tool that uses the same package manager.

Fix the environment that owns npm

Install Node.js and npm through Ubuntu when you need a system-wide command, or keep npm under your user account when Node.js came from a version manager and run project and global development commands without sudo.

Frequently asked questions

The answers separate a missing installation from a sudo path that cannot see a user-managed Node.js installation.

Why does sudo say npm is not found when npm works without sudo?

Node version managers can install npm under your user home directory. sudo runs with a restricted environment, so it may not receive the directory that contains npm. Run npm without sudo for project dependencies, or install Node.js through a system package source when you need a system-wide command.

Should I run sudo npm install -g?

Avoid it for normal development. A global install with sudo can create root-owned files in npm directories. Use a user-managed Node.js installation and its global package location, then reserve sudo for operating-system package management.

Does installing nodejs always install npm on Ubuntu?

It depends on the package source and Ubuntu release. Check both commands after installation. If node works but npm does not, install the npm package from the same package source or reinstall Node.js with a supported Node.js distribution.