To fix the "zsh: command not found: npm" error on a Mac, you must either install Node.js (which includes npm) or add the existing Node.js installation path to your Zsh configuration file. This error occurs because the Zsh shell cannot locate the npm executable in the directories listed in your system's PATH environment variable.

What You'll Need

  • A Mac running macOS (Catalina or newer, as Zsh is the default shell).
  • An active internet connection to download installers or packages.
  • Administrative privileges (sudo access) to install software and edit system files.
  • The Terminal app (found in Applications > Utilities).

Step-by-Step

  1. Verify if Node.js is actually installed. Open your Terminal and type node -v. If this also returns "command not found," Node.js is not installed on your system, or its path is completely missing. If it returns a version number (e.g., v20.10.0) but npm -v still fails, your installation is corrupted or the npm binary is separated from the node binary in your PATH.
  2. Install Node.js using the official installer. If Node.js is missing, the most straightforward method is to visit the official Node.js website. Download the macOS Installer (.pkg) for the LTS (Long Term Support) version. Run the installer and follow the prompts. The installer automatically attempts to set up the necessary symbolic links in /usr/local/bin, which is a standard directory that Zsh checks by default.
  3. Alternative Installation via Homebrew. If you prefer a package manager, use Homebrew. If you have Homebrew installed, run brew install node. Homebrew manages paths more dynamically and often resolves permission issues that occur with the .pkg installer. Once the process finishes, restart your Terminal to refresh the environment.
  4. Locate your npm installation path. If you know Node.js is installed but npm is not found, you need to find where it lives. Common paths include /usr/local/bin/npm or, if using a version manager like NVM, a hidden folder in your home directory (e.g., ~/.nvm/versions/node/vX.X.X/bin). Use the command find /usr/local/bin -name "npm" to check common areas.
  5. Edit the .zshrc configuration file. Zsh reads a configuration file called .zshrc every time a new terminal window opens. You must tell Zsh where to look for npm. Type nano ~/.zshrc in the Terminal to open the file in a text editor.
  6. Add the PATH export line. Scroll to the bottom of the file. Add the following line, replacing the path with the actual location of your node binaries if it differs from the standard: export PATH="/usr/local/bin:$PATH". If you are using NVM, ensure the NVM initialization script is present in this file. It usually looks like a block of code starting with export NVM_DIR="$HOME/.nvm".
  7. Save and exit the editor. In the nano editor, press Control + O to write the changes, then press Enter to confirm the filename. Press Control + X to exit the editor and return to the command prompt.
  8. Refresh the Zsh shell. The changes to .zshrc do not apply to currently open windows automatically. You must force the shell to reload the configuration by typing source ~/.zshrc. Alternatively, you can simply close the Terminal app entirely and reopen it.
  9. Final Verification. Run npm -v. You should now see a version number instead of the "command not found" error. You can also run which npm to see exactly which file path the shell is using to execute the command.

Common Mistakes

One of the most frequent errors is forgetting to run the source ~/.zshrc command after editing the configuration file. Many users assume that saving the file is enough, but the current shell session only loads the PATH variable once upon startup. Without sourcing the file or restarting the terminal, the error will persist despite the fix being written to the disk.

Another common mistake is using sudo to install npm packages globally (e.g., sudo npm install -g package-name). While this might solve a temporary "permission denied" error, it often leads to ownership conflicts where the Zsh shell cannot access the binaries because they are owned by the root user rather than the local user account. This can eventually lead to paths becoming broken or binaries becoming unexecutable.

Users also frequently confuse .zshrc with .bash_profile. Older macOS versions used Bash as the default shell, and many online tutorials still suggest editing .bash_profile or .bashrc. Since macOS Catalina, the default is Zsh. Any paths added to Bash profiles are ignored by Zsh, leaving the user wondering why their changes aren't working.

Tips

To avoid path issues in the future, consider using a Node Version Manager (NVM). NVM allows you to install multiple versions of Node.js and npm on a single machine and switch between them seamlessly. Because NVM manages the PATH automatically within the .zshrc file, it eliminates the need to manually edit system directories or deal with /usr/local/bin permissions.

When adding paths to your .zshrc, always append :$PATH to the end of your export statement. For example: export PATH="/my/new/path:$PATH". This ensures that you are adding a new directory to the existing list of paths rather than overwriting the entire list. If you overwrite the PATH, basic system commands like ls, cd, and mkdir will suddenly stop working, forcing you to use absolute paths to fix the mistake.

Regularly check your Node and npm versions using node -v and npm -v after macOS system updates. Occasionally, major OS updates can reset certain system permissions or modify the default shell behavior, which may require you to re-source your configuration or reinstall the Node binaries.

Troubleshooting

If you have followed the steps and still see "command not found," first check if the file .zshrc actually exists. Some Macs do not create this file by default. If it doesn't exist, the nano ~/.zshrc command creates a new one, but you must ensure you are saving it correctly. Run ls -a ~ to see all hidden files in your home directory and confirm .zshrc is listed.

If you are using an Apple Silicon (M1, M2, M3) Mac, Homebrew installs binaries into /opt/homebrew/bin instead of /usr/local/bin. If you used Homebrew, ensure your PATH export line reflects this: export PATH="/opt/homebrew/bin:$PATH". This is a very common point of failure for users migrating from Intel-based Macs to Apple Silicon.

Check for syntax errors in your .zshrc file. A missing quote mark or a misplaced space in the export command can cause the shell to ignore the entire line or fail to load the configuration. If you see an error message when running source ~/.zshrc, reopen the file and verify that the line begins exactly with export PATH= and that the paths are enclosed in double quotes.

If all else fails, perform a clean reinstall. Delete the existing Node folder (usually in /usr/local/lib/node_modules and /usr/local/bin/node) and the npm cache. Then, restart the Mac and use the official .pkg installer again. This clears out any corrupted symbolic links that might be pointing to non-existent directories.

Frequently Asked Questions

Why did npm stop working after a macOS update?

macOS updates can sometimes reset system environment variables or change permissions in the /usr/local/bin directory. This prevents Zsh from locating the npm executable even if the files are still present on the drive.

Is npm different from Node.js?

Node.js is the runtime environment that allows you to run JavaScript on your computer. npm (Node Package Manager) is the tool used to install libraries and dependencies for your projects. They are bundled together in the standard installer.

Do I need to add npm to the PATH every time I restart?

No. By adding the export command to the .zshrc file, you are making the change permanent. Zsh loads this file every time a new session begins, so the path is automatically set.

What is the difference between .zshrc and .zprofile?

.zshrc is executed for interactive shells (every time you open a terminal), while .zprofile is executed only during login shells. For most users, putting PATH exports in .zshrc is the most reliable method.

Can I have multiple versions of npm installed?

Yes, but managing them manually is difficult. It is highly recommended to use NVM (Node Version Manager), which allows you to switch between versions without manually editing your PATH variables.