VMHeaven

Troubleshooting

npx: command not found — fix it on Linux, macOS and Windows

npx ships with npm, so this error nearly always means Node.js is missing or not on your PATH. Fixes for bash, zsh, nvm, sudo and Docker.

Updated 08 Aug 2026~6 min read

npx is not a separate program you can install. It has shipped as part of npm since npm 5.2, and npm ships with Node.js. So when the shell says bash: npx: command not found, the real message is almost always: Node.js is not installed, or it is installed somewhere this shell cannot see.

Start by finding out which of the two it is.

diagnose
command -v node    # path to node, or nothing
command -v npm     # path to npm, or nothing
node --version
npm --version
  • Nothing prints at all — Node is missing. Install it.
  • node and npm work, npx does not — your npm predates 5.2 or the shim was removed. Update npm.
  • It works in one terminal but not another — a PATH problem, most often nvm.

Installing Node.js properly

Debian and Ubuntu — NodeSource

This installs Node system-wide, which is what you want on a server where services run as another user.

Debian · Ubuntu
sudo apt update && sudo apt install -y ca-certificates curl gnupg
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs

node --version && npm --version && npx --version

Any Linux or macOS — nvm

nvm installs Node into your home directory and lets you switch versions per project. Best for a development machine, poor for a service account.

nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
# reload the shell so nvm is on the PATH
source ~/.bashrc      # or ~/.zshrc

nvm install --lts
nvm use --lts

Alpine and RHEL family

Alpine · Rocky · Alma
# Alpine
apk add --no-cache nodejs npm

# Rocky, AlmaLinux, RHEL, Fedora
sudo dnf module enable -y nodejs:22
sudo dnf install -y nodejs npm

“zsh: command not found: npx” after installing

This is the single most common variant of the problem, and it is not a zsh bug. The nvm installer appends its setup block to ~/.bashrc. If your login shell is zsh — the default on macOS and on many modern setups — that file is never read.

~/.zshrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

Add those lines, open a new terminal, and check with which npx. The same applies in reverse if you installed under zsh and later switched to bash.

“sudo: npx: command not found”

Here npx genuinely exists for your user and genuinely does not exist for root. sudo resets the environment for safety, so a Node living in ~/.nvm disappears.

workaround
sudo -E env "PATH=$PATH" npx <command>

Treat that as a stopgap. If something needs to run as root regularly — a build step, a service, a cron job — install Node system-wide with NodeSource instead. Running npm packages as root is worth avoiding anyway: install scripts execute with whatever rights you give them.

In Docker and CI

A container built FROM debian or FROM alpine has no Node at all. Use an official Node image rather than installing it by hand — the image is smaller and the version is pinned.

Dockerfile
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
CMD ["npm", "start"]

npm works but npx does not

Rare, but it happens on long-lived servers. Check the npm version — anything below 5.2 has no npx at all, and some hand-rolled installs drop the shim.

repair
npm --version
sudo npm install -g npm@latest

# if the shim is still missing
sudo npm install -g npx

Running Node on a server

Once npx works, the next thing that usually bites is the process dying when the SSH session closes. Use a systemd unit rather than nohup or a screen session — it restarts the app after a crash and after a reboot. If you are setting up a fresh box for this, our Hi-CPU KVM line is built for single-thread-heavy work like Node builds.

Frequently asked

Do I have to install npx separately?

No. npx has shipped as part of npm since npm 5.2. If npm works but npx does not, your npm is either ancient or the shim is missing from the install.

Why does npx work for my user but not under sudo?

sudo resets the environment, so a Node installed through nvm is not on the root PATH. Use 'sudo -E env PATH=$PATH npx …' or install Node system-wide.

Which Node version should I install on a server?

The current LTS. It gets security fixes for about 30 months and every major framework targets it.

More in Troubleshooting

See all