DEV Community

adiozdaniel
adiozdaniel

Posted on

πŸš€ Fixing "bash: ng: command not found" – A Guide for Angular Developers

πŸš€ 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
Enter fullscreen mode Exit fullscreen mode

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:

  1. Open your shell config: nano ~/.bashrc # or ~/.zshrc``
  2. 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
`

  1. 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:


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:

  1. Install Angular CLI (npm install -g @angular/cli)
  2. Fix shell configs (remove/make autocompletion conditional)
  3. Check Node.js/npm

✨ Now you’re ready to code without errors! ✨


πŸ“š Further Reading


🎨 Icons Used:

  • πŸš€ = Quick Fix
  • πŸ” = Diagnosis
  • πŸ›  = Solution
  • βœ”οΈ = Best Practice
  • ⚠ = Warning

Top comments (0)