π Fixing "bash: ng: command not found" β A Guide for Angular Developers
π Introduction
If you're an Angular developer working on Linux/macOS/WSL, you might encounter:
β bash: ng: command not found
This error appears when:
Angular CLI (
ng
) is not installed.Your shell (bash/zsh) tries loading CLI autocompletion too early.
Node.js/npm is misconfigured.
π In this guide, weβll cover:
β
Why this happens
β
Proper Angular CLI installation
β
Shell config fixes
β
Best practices
1. π Why Does This Error Happen?
π Case 1: Angular CLI Not Installed
The simplest reasonβ@angular/cli
isnβt installed globally.
π Fix:
npm install -g @angular/cli
β Verify:
ng version # Should show Angular CLI version
π Case 2: Shell Autocompletion Loading Too Early
Some .bashrc
/.zshrc
files include:
source <(ng completion script) # β Runs before 'ng' exists!
This causes the error if ng
isnβt installed yet.
π Fix:
- Open your shell config:
nano ~/.bashrc # or ~/.zshrc
`` - Remove the line or make it conditional:
`bash
if command -v ng &> /dev/null; then
source <(ng completion script) # β
Only runs if 'ng' exists
fi
`
-
Reload:
source ~/.bashrc
π Case 3: Node.js/npm Issues
If npm
fails, ng
wonβt install.
π Verify Node.js & npm:
`bash
node -v # Should show v16+
npm -v # Should show 8+
`
β No Node.js? Install it via:
- NVM (recommended)
- Official Node.js
2. π‘ Best Practices for Angular CLI
βοΈ 1. Install Angular CLI Globally
npm install -g @angular/cli
βοΈ 2. Use npx
for Project-Specific Installs
Avoid global installs with:
npx @angular/cli new my-project
βοΈ 3. Safe Autocompletion Setup
`bash
Only enable if Angular CLI exists
if command -v ng &> /dev/null; then
source <(ng completion script)
fi
`
βοΈ 4. Fix PATH Issues
If ng
is installed but not found:
`bash
Check npm global install path
npm config get prefix
Add to PATH (if missing)
export PATH="$PATH:$(npm config get prefix)/bin"
`
3. π Troubleshooting Checklist
Issue | Solution |
---|---|
β ng: command not found |
npm install -g @angular/cli |
β Shell autocompletion error |
Remove source <(ng completion script) or make it conditional
|
π§ Node.js missing |
Install via NVM |
π« npm not working |
Reinstall Node.js or check PATH
|
π Permission denied |
Fix npm permissions (Guide) |
π― Conclusion
Fixing ng: command not found
is simple once you diagnose the cause:
-
Install Angular CLI (
npm install -g @angular/cli
) - Fix shell configs (remove/make autocompletion conditional)
- Check Node.js/npm
β¨ Now youβre ready to code without errors! β¨
π Further Reading
- Angular CLI Docs π
- NVM Guide π
- npm Permission Fixes π§
π¨ Icons Used:
- π = Quick Fix
- π = Diagnosis
- π = Solution
- βοΈ = Best Practice
- β = Warning
Top comments (0)