Check What Is Installed
command -v node npm
node -v
echo "$PATH"
command -v prints the path of each program it finds and nothing for a missing one. The result points to one of four cases:
- Neither is found: Node.js is not installed, at least not for this user.
- node is found, npm is not: Node.js came from the distribution without its npm package.
- Works in one terminal, not in another: nvm is installed but not loaded.
- Works for you, not in sudo, cron, or systemd: those run with a different PATH.
How the shell searches PATH in general is explained in command not found in Linux.
Node.js Is Not Installed
Install it in one of the ways described in how to install Node.js on Ubuntu: from the distribution, from the NodeSource repository, or with nvm. The quickest is the distribution:
sudo apt update
sudo apt install nodejs npm
On AlmaLinux and Rocky Linux, install nodejs with dnf; if npm is still missing afterwards, install the npm package as well.
npm Is a Separate Package on Debian and Ubuntu
The distribution's nodejs package does not include npm. Install it separately:
sudo apt install npm
The nodejs package from the NodeSource repository already contains npm, so do not add the distribution's npm on top of it. The distribution's Node.js can be several major versions behind; how to update Node.js shows how to move to a newer one.
Installed with nvm, but Not Loaded
nvm installs Node.js under ~/.nvm in your home folder and adds lines to ~/.bashrc that load it. Until those lines run, the shell knows neither node nor npm. After installing, open a new terminal or run:
source ~/.bashrc
nvm ls
nvm install --lts
nvm alias default 'lts/*'
nvm ls shows the installed versions. The last line makes a version the default for new shells. nvm works per user: Node.js installed with nvm as root is not available to the deploy user, and the other way round.
Cron, systemd, and Remote Commands
Cron jobs, systemd services, and commands sent with ssh user@your_server_ip 'npm -v' do not run an interactive shell, so the nvm lines in ~/.bashrc are often skipped. Give them the full path. Find it once in a normal shell:
command -v npm
The npm program starts node through PATH, so a full path to npm alone is not enough: put the folder on PATH. In a crontab, with VERSION replaced by the folder name from ls ~/.nvm/versions/node:
PATH=/home/deploy/.nvm/versions/node/VERSION/bin:/usr/local/bin:/usr/bin:/bin
0 3 * * * cd /srv/app && npm run report
In a systemd unit, set Environment=PATH=… the same way and give ExecStart the full path; see systemd unit files. The path changes whenever you switch Node.js versions, so for services a system-wide install is easier to maintain. More on PATH in environment variables in Linux.
sudo npm: command not found
sudo replaces your PATH with a fixed list, secure_path, set in the sudoers file. That list contains the system folders, not ~/.nvm. With nvm, do not use sudo at all: global packages go into your home folder, so npm install -g works as your own user. With a system-wide install in /usr/bin, sudo finds npm normally.
Check the Result
hash -r
command -v npm
npm -v
hash -r clears the locations that the shell has remembered for commands, which matters after you install or remove a program in an open terminal.