Mac系统bash提示‘ng command not found’求助:仅刚安装时ng serve可用
This is a super common hiccup when you install Angular CLI globally—your terminal only temporarily adds the ng command to your system's PATH during the installation session. When you fire up a new terminal, that temporary PATH change doesn't carry over. Here's how to lock in the fix permanently:
Step 1: Locate where ng is installed
First, hop back to the terminal where ng serve works, and run this command to get the full path to the ng executable:
which ng
You’ll get output like /usr/local/bin/ng or ~/.nvm/versions/node/v18.17.0/bin/ng—we only need the directory part (e.g., /usr/local/bin or ~/.nvm/versions/node/v18.17.0/bin).
If you installed Angular via npm, you can also get npm’s global prefix directly with:
npm config get prefix
Just add /bin to the end of that result (for example, if the prefix is /usr/local, use /usr/local/bin).
Step 2: Add the directory to your bash PATH permanently
Since you’re using bash, we’ll edit your bash profile file to make this change stick forever.
- Open your bash profile in a text editor:
open -e ~/.bash_profile
If the file doesn’t exist yet, create it first with touch ~/.bash_profile.
- Add this line at the end of the file, replacing
[PATH_TO_NG_DIRECTORY]with the directory you found in Step 1:
export PATH="$PATH:[PATH_TO_NG_DIRECTORY]"
For example, if your ng lives in /usr/local/bin, the line becomes:
export PATH="$PATH:/usr/local/bin"
- Save the file and close the text editor.
Step 3: Verify the fix
Either restart your terminal, or run this command to apply the changes right away in your current terminal:
source ~/.bash_profile
Now fire up a new terminal and run ng --version—it should work without the "command not found" error!
Quick note if you switch to zsh later
If you ever switch to zsh (Mac’s default shell now), you’ll need to edit ~/.zshrc instead of ~/.bash_profile using the exact same steps above.
内容的提问来源于stack exchange,提问作者Jyothi




