If you are developing on a MacBook, you may encounter issues where Node.js and npm commands are recognized in certain directories but not in others. This can lead to frustrating errors, such as:
zsh: command not found: npm
zsh: command not found: node
This article will guide you through resolving this issue by ensuring that npm and Node.js are properly installed and configured in your terminal, regardless of the directory you’re working in.
Understanding the Issue
The error occurs when the npm and Node.js executables are not found in your system’s PATH
. The PATH
is an environment variable that tells your system where to look for executables. If npm or Node.js are not properly included in your PATH
, the terminal won’t be able to find them in certain directories, resulting in the error:
zsh: command not found: npm
However, when you switch to another directory, the commands might work as expected, which means npm and Node.js are installed but not accessible from all paths.
Step-by-Step Guide to Fixing the Issue
Here’s a complete guide to fixing this issue:
1. Check if Node.js and npm are installed
First, verify if Node.js and npm are globally installed and accessible from your system. To do this, open your terminal and run the following commands:
which node
which npm
If Node.js and npm are installed, these commands should return the path to the executables. For example:
/usr/local/bin/node
/usr/local/bin/npm
If no path is returned, you may need to install Node.js and npm, which we’ll cover later in this article.
2. Add Node.js and npm to Your PATH
If the commands work in some directories but not others, it’s likely that Node.js and npm are not correctly included in your shell’s PATH
. You can resolve this by manually adding them.
Here’s how to add Node.js and npm to your PATH
in zsh:
- Open your terminal.
- Use a text editor (e.g.,
nano
) to open the.zshrc
file, which configures your zsh shell environment:
nano ~/.zshrc
- Add the following line at the bottom of the
.zshrc
file. This will ensure that npm and Node.js are included in thePATH
:
export PATH="/usr/local/bin:/usr/local/sbin:$(npm bin -g):$PATH"
- Save and exit the file:
- Press
CTRL + O
to save the changes. - Press
Enter
to confirm the file name. - Press
CTRL + X
to exit the editor.
- Apply the changes to your current terminal session by running:
source ~/.zshrc
This will reload the terminal configuration, and your PATH
will be updated to include Node.js and npm.
3. Reinstall Node.js Using Homebrew (if necessary)
If you continue to experience issues or suspect that Node.js and npm are not properly installed, it might be best to reinstall them using Homebrew, a popular package manager for macOS. Here’s how:
- Uninstall Node.js:
brew uninstall node
- Reinstall Node.js:
brew install node
Homebrew ensures that Node.js and npm are installed correctly and placed in the right directories.
4. Verify Installation
After updating your PATH
or reinstalling Node.js and npm, you can verify that everything is working by checking the versions of both:
node -v
npm -v
This should output the installed versions of Node.js and npm, such as:
v20.17.0
10.8.3
If these commands return version numbers, it means that Node.js and npm are now accessible from your terminal.
5. Restart Your Terminal
If everything seems correct but you’re still facing issues, try restarting your terminal. Sometimes, changes to the PATH
or shell configurations don’t fully apply until the terminal is restarted.
Final Thoughts
The “command not found” issue for npm and Node.js can be frustrating, but it’s usually a matter of configuring your environment properly. By ensuring that npm and Node.js are correctly installed and added to your shell’s PATH
, you’ll have a more seamless development experience.
If you encounter further issues, consider reinstalling npm and Node.js using Homebrew, as it’s a reliable package manager for macOS systems like the M1.
Need More Help?
Feel free to reach out in the comments section if you need further clarification or run into additional issues.
Thanks for reading…
Happy coding!