Mac下npm全局安装模块后命令无法找到的问题求助
Hey there! Let’s work through this frustrating issue where your globally installed npm commands (like remotebuild and ng) are throwing "command not found" errors. This almost always comes down to your system’s PATH missing the directory where npm stores global executable files, or small permission hiccups. Here’s how to fix it step by step:
Step 1: Locate npm’s Global Bin Directory
First, find out where npm installs global packages and their executable files. Run this command in your terminal:
npm config get prefix
This will return a path (e.g., /Users/YourUsername/.npm-global or /usr/local). The commands you’re missing live in the bin subfolder of this path—so if your prefix is ~/.npm-global, the bin directory is ~/.npm-global/bin.
Step 2: Check if This Directory is in Your PATH
Next, verify if this bin directory is included in your system’s PATH (the list of locations your terminal checks for commands):
echo $PATH
Look for the bin path from Step 1 in the output. If you don’t see it, that’s the root of your problem.
Step 3: Add the Bin Directory to Your PATH
You need to add the missing path to your shell’s configuration file. Which file you edit depends on the shell you’re using:
- For Bash: Edit
~/.bash_profileor~/.bashrc - For Zsh (default on newer Macs): Edit
~/.zshrc
Open the file in a text editor (e.g., nano ~/.zshrc), then add this line at the bottom (replace the path with your actual bin directory from Step 1):
export PATH="$HOME/.npm-global/bin:$PATH"
Save the file (for nano, press Ctrl+O, then Enter, then Ctrl+X to exit).
Step 4: Reload Your Shell Configuration
Make the changes take effect immediately by reloading your shell config:
# For Bash source ~/.bash_profile # For Zsh source ~/.zshrc
Step 5: Test the Commands
Now try running remotebuild or ng -v again—they should work!
Bonus: Fix Permission Issues & Avoid Using sudo
Using sudo to install global npm packages can cause permission conflicts (like the EACCES error you saw earlier). To avoid this, set npm to install global packages in your user directory instead:
- Set the global prefix to a user-owned folder:
npm config set prefix '~/.npm-global' - Follow Steps 3-4 above to add
~/.npm-global/binto your PATH (if you haven’t already) - Now you can install global packages without
sudo:npm install -g remotebuild npm install -g @angular/cli@latest
If You’re Using NVM
If you installed Node via NVM, double-check that NVM is properly loaded in your shell config. Run nvm use v8.11.2 to switch to your Node version, then run echo $PATH again—you should see a path like ~/.nvm/versions/node/v8.11.2/bin in the output. If not, ensure you have the NVM initialization line in your shell config (it’s usually added automatically when you install NVM, but sometimes gets missed).
Quick Permission Check for Executables
If even running the command directly from the bin directory fails, check if the file has execute permissions:
ls -la ~/.npm-global/lib/node_modules/remotebuild/bin/remotebuild
Look for an x in the permissions column (e.g., -rwxr-xr-x). If there’s no x, add execute permissions with:
chmod +x ~/.npm-global/lib/node_modules/remotebuild/bin/remotebuild
内容的提问来源于stack exchange,提问作者bkhines

