The “ng: command not found” error in Bash indicates that the Angular CLI is either not installed or inaccessible. To fix this error, install Angular CLI using:
sudo apt update && sudo apt install -y nodejs npm
sudo npm install -g @angular/cli
The “ng” is a powerful tool for developing Angular applications. It provides a set of commands and functionalities to scaffold, build, test, and deploy Angular applications. However, encountering “bash: ng: command not found” indicates that either the Angular command-line tool is not installed on your system or the PATH variable is not set for ng. Let’s see the reasons for this error in detail and explore the possible fixes.
Reasons for “ng: command not found” Error in Bash
Some common reasons for the “ng” command not found error are:
- Angular CLI not Installed: If you are unable to use the “ng” command, it could be that your system does not have Angular CLI installed.
- js and npm not Installed: Angular CLI depends on Node.js and npm. Make sure they are installed on your system.
- Incorrect Path: The PATH on your system may not contain “ng”.
- Inadequate Permissions: “ng” might not have the authorization it needs to function.
- Binary Not Found: The Angular binary might be missing or corrupted.
4 Methods to Solve “bash: ng: command not found” Error
Follow the below methods to solve the ng command not found error in Bash:
1. Install Angular CLI
Most of the time, the absence of Angular CLI on your system causes the “bash: ng: command not found” error. In this instance, APT in Ubuntu, YUM in RedHat, and Pacman in Arch Linux can install it on your system. To do that, follow the steps:
- Press CTRL+ALT+T to open the terminal in Ubuntu.
- Update the repository using
sudo apt update
. - Run the command to install npm (Node Package Manager) in Ubuntu:
sudo apt install -y nodejs npm
- Now, run the below command to install Angular CLI globally:
sudo npm install -g @angular/cli
- Execute
ng --version
to verify the installation.
Now, the ng command is executed successfully.
2. Link “npm” to Angular CLI
To fix the “ng: command not found” error, you can create a link between npm and Angular CLI. Here’s how to do it:
- Go to the directory where your Angular CLI project is located.
- Run the following command to link the Angular CLI globally:
npm link @angular/cli
The above command establishes a symbolic link between the Angular CLI package and the npm global directory, allowing you to use the ng command globally from any terminal session.
3. Set “ng” PATH Variable
A prevalent reason behind the “ng command not found” is it is not present in your system PATH environment variables. To set the PATH variable for “ng”, follow the steps below:
- Press CTRL+ALT+T to open the terminal.
- Use which command to check the current path of ng:
which ng
- Open bashrc file using:
nano ~/.bashrc
- Now, replace <path to ng> in
export PATH=$PATH:<path to ng>
code with the which command’s output and paste it at the end of the file. For example:export PATH=$PATH:/usr/local/bin/ng
- Press CTRL+S to save, and CTRL+X to exit.
- Then, execute the .bashrc file using:
source ~/.bashrc
- Now you can check running a ng command. For example:
ng --version
After adding the path, the ng command ran without raising any error.
4. Set an Alias to “ng”
To fix the “ng: command not found” error, you can create a temporary solution by setting up an alias for ng. Here’s how you can do it:
echo "alias ng='$(which ng)'" >> ~/.bashrc; . ~/.bashrc
This command appends an alias for the ng command to the ~/.bashrc file and immediately applies the changes in the current terminal session. The alias ensures that the ng command points to the correct path by using the $(which ng)
syntax, which dynamically retrieves the actual path to the ng executable.
How to Solve “ng command not found” Error in MacOS?
To solve “ng: command not found” error in MacOS, follow the steps below:
-
First, open your Mac terminal and run
ng --version
to check whether Angular is installed or not. If Angular CLI is installed, this command will display information about the installed Angular CLI version. - If it’s not installed, run the below command to install it:
npm install -g @angular/cli
However, if you get the error even after installing Angular CLI, here’s how to solve it:
- Open the paths file using:
sudo vim ~/etc/paths
- Enter the user password if prompted.
- Press Fn+i to enable insert mode.
- Paste the below line at the end:
~/.npm-global/bin
- Press ESC and type :wq to save the file.
Conclusion
In conclusion, resolving the “bash: ng: command not found” issue involves ensuring the installation of Angular CLI and configuring the system’s PATH appropriately. Applying these solutions allows you to seamlessly access ng commands within the Bash environment and streamline various aspects of Angular development which are an integral part of the Angular development workflow.
People Also Ask
What to do when ng command is not working?
Ensure Angular CLI is installed globally If not, install it using npm install -g @angular/cli
. Then confirm the Angular CLI installation using ng --version
. Also, you can run echo $PATH
to ensure the npm global binary directory is in the PATH.
How to install ng for Angular?
To install Angular CLI (ng), open a terminal or command prompt and run sudo npm install -g @angular/cli
. After that, use ng --version
to confirm the installation by checking the Angular CLI version.
What is the Ng command?
The ng command is associated with the Angular CLI (Command Line Interface). It streamlines various tasks in Angular development, such as project creation, component generation, server start, building, testing, and more. It streamlines Angular development tasks by providing a set of commands to manage and scaffold Angular projects efficiently.
How do I use ng command locally?
To use the ng command locally in an Angular project, open a terminal and navigate to your Angular project’s root directory. Then, execute ng commands directly within your project, for example: ng generate component my-component
. This runs the ng generate command locally for your Angular project.
What is the use of ng command?
The ng command is the Angular CLI (Command Line Interface) used for various tasks in Angular development, including:
- Create new Angular projects:
ng new <project-name>
- Generate components:
ng generate component <component-name>
- Generate modules:
ng generate module <module-name>
- Generate services:
ng generate service <service-name>
- Start a development server:
ng serve
- Build the application for deployment:
ng build
- Run unit tests:
ng test
- Lint code for style and syntax:
ng lint
How to Solve “sudo: ng: command not found” in Linux?
To solve “sudo: ng: command not found” in Linux, use sudo with full path. The syntax is:
sudo $(which ng) [command]
If you still find the error, follow the steps:
- Install nodejs and npm:
sudo apt update && sudo apt install -y nodejs npm
- Install Angular CLI globally:
sudo npm install -g @angular/cli
- Confirm npm global binary directory is in sudo’s secure_path.
Related Articles
- [Fixed] “bash: sudo: command not found” Error
- [Solved] Bash “yarn” Command Not Found Error
- [Solved] Bash “docker” Command Not Found Error
- [Fixed] “bash: java: command not found” Error
- Solved “bash: yum: command not found” [RHEL, Fedora, CentOS]
- [Solved] “bash: vim: command not found” Error
- [Fixed] “bash: curl: command not found” Error
- [Solved] “bash: node: command not found” Error
- [Solved] “bash: code: command not found” Error
- [Fixed!] “systemctl command not found” Error in Linux
<< Go Back to [Solved] “bash: command not found” Error | Bash Error Handling and Debugging | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners