Skip to content

How to Fix 'command not found: npm'

DodaTech 3 min read

In this tutorial, you'll learn about How to Fix 'command not found: npm. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

You type npm install and get:

bash: npm: command not found

Node.js and npm aren't installed on your system.

What is npm exactly?

npm comes bundled with Node.js. When you see "command not found: npm", either Node.js is not installed, or the terminal cannot find the npm executable because it is not in your PATH.

Which install method is right for you?

There are four ways to get npm: the NodeSource Repository (Linux), Homebrew (macOS), the official installer (Windows), and nvm (multiple versions). Choose based on your operating system.

Quick Fix

Linux (Ubuntu/Debian)

curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt install -y nodejs

Verify:

node --version && npm --version

Expected output:

v22.x.x
10.x.x

The NodeSource script adds the official Node.js Repository and imports the GPG key. The -E flag preserves environment variables with sudo. After the script runs, apt install nodejs installs both Node.js and npm together. This is recommended for Ubuntu and Debian because you get automatic updates through apt.

macOS

Using Homebrew:

brew install node

Or download from nodejs.org.

Homebrew automatically adds Node.js and npm to your PATH. If you prefer not to use Homebrew, download the macOS installer from nodejs.org — it is a standard .pkg file that guides you through the installation with a visual wizard.

Windows

  1. Download the LTS installer from nodejs.org
  2. Run the installer (check "Add to PATH")
  3. Restart your terminal

The "Add to PATH" checkbox is easy to miss — without it, npm will still show "command not found" even after installation. If you forgot to check it, you can either reinstall or manually add the Node.js directory to your PATH through System Environment Variables.

If npm Is Already Installed

Sometimes npm is installed but not in PATH:

# Find where npm lives
which npm
# If it's there but not working, add to PATH:
export PATH="/usr/local/bin:$PATH"

Add the export to ~/.bashrc or ~/.zshrc to make it permanent.

The which npm command shows the full path to the npm binary. If it returns a path like /usr/local/bin/npm, npm is installed but your shell is not looking in that directory. The export PATH command adds it temporarily. To make it permanent, add the same line to your shell configuration file — then every new terminal session will include /usr/local/bin in the search path.

Avoid permission issues entirely with nvm:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash
# Restart terminal, then:
nvm install --lts
nvm use --lts

nvm installs Node.js in your home directory, which means you never need sudo for npm global installs. It also lets you switch between Node.js versions with a single command, which is essential if you work on projects that require different versions. After installing nvm, you can run nvm ls-remote to see all available versions and nvm install <version> to install any specific one.

Prevention

Always use a version manager. Installing Node.js directly with apt or the official installer puts it in system directories, which can cause permission conflicts when running npm install -g. Using nvm or fnm avoids this entirely.

Add npm to your PATH during installation. If you use the Windows installer, check the "Add to PATH" option. For Linux and macOS, the package manager or nvm handles this automatically.

Restart your terminal after installing. The PATH is loaded when the shell starts. If you installed npm but the terminal session was opened before the installation, the new PATH entry is not loaded. Reload the shell or open a new terminal window.

Use a .nvmrc file in your projects. This file specifies the Node.js version for the project. When teammates run nvm use in the project directory, nvm automatically switches to the correct version, preventing "works on my machine" issues.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro