Ubuntu 16.04中ng命令无法运行的解决求助
ng Command Not Working on Ubuntu 16.04 Hey there! I’ve dealt with this exact ng command issue on Ubuntu before—let’s get your Angular project up and running smoothly. Here’s a step-by-step breakdown of what to do:
1. Fix the Node.js Alias Issue
Ubuntu’s default Node.js package installs as nodejs instead of node, which can break tools like Angular CLI that expect the node command. Let’s create a symbolic link to fix this:
sudo ln -s /usr/bin/nodejs /usr/bin/node
Verify it worked by running:
node -v npm -v
You should see version numbers for both if the link was created correctly.
2. Ensure npm’s Global Bin Path is in Your System PATH
Global npm packages (like Angular CLI) are installed to a bin directory that needs to be in your system’s PATH. First, find where npm installs global packages:
npm config get prefix
This will return a path (usually /usr/local or ~/.npm-global). Next, check if the bin subdirectory of that path is in your PATH:
echo $PATH
If you don’t see it, add it to your shell configuration file. For example, if your prefix is ~/.npm-global, edit ~/.bashrc (or ~/.profile) and add this line:
export PATH=~/.npm-global/bin:$PATH
Then apply the change immediately:
source ~/.bashrc
3. Reinstall Angular CLI Properly
Sometimes a corrupted or incomplete installation causes the ng command to fail. Let’s clean up and reinstall:
First, uninstall any existing Angular CLI versions:
sudo npm uninstall -g angular-cli sudo npm uninstall -g @angular/cli sudo npm cache clean --force
Then install the latest stable version globally:
sudo npm install -g @angular/cli
4. Use the Local Project ng Command (If Global Still Fails)
If the global ng command still doesn’t work, you can use the local version installed in your project’s node_modules. Since your package.json already has the ng script defined, run:
npm run ng serve
Or directly call the local executable:
./node_modules/.bin/ng serve
5. Verify the Fix
Once you’ve completed the steps above, test if ng works by running:
ng version
If you see the Angular CLI version, Node.js version, and other details, you’re good to go!
内容的提问来源于stack exchange,提问作者Niladri Banerjee - Uttarpara

