How to Fix “ng: command not found”

AngularJS is a tool that can be installed on systems such as Ubuntu which allows various libraries and extensions to work efficiently. The keyword “ng” is part of the Angular JS program and is used to perform various tasks related to the Angular JS files. While attempting to utilize this keyword, the “ng: command not found” problem may be encountered.

This article will state the reasons behind this error and also demonstrate how this error can be resolved on your system.

How to Resolve the “ng: command not found”?

As it goes for every “command not found” error, there are multiple reasons that are invoking this error. In this section, these reasons and their solutions will be tackled in detail.

Reason 1: Angular CLI not Installed

The first and most common reason for this error to pop up is that the Angular CLI package is not installed on the system. Since the ng command is part of this package, the problem will be invoked. Check out the example below:

Solution: Install the Angular CLI

The most simple solution to this issue is to install the Angular CLI package on the system using the following command:

$ sudo npm install -g @angular/cli

Once the Angular CLI has been installed onto the system, you can run the “ng” command again in the terminal and you will see that the error did not occur. The command and its example are shown below:

$ ng version

Reason 2: Link is Missing Between npm and Angular CLI

Another possible reason revolves around whether the Angular CLI and the npm are properly linked together. If they are not linked together, npm will be unable to find the ng since the ng command exists within the Angular CLI package.

Solution: Link npm to Angular CLI

The only possible solution to this issue is to link the Angular CLI package to the npm which is another package manager tool. This can be achieved by running the command shown below in the terminal:

$ sudo npm link @angular/cli

Below is the output that will represent the successful linking of npm and the Angular CLI:

Reason 3: Path Variable is Missing

Usually while installing any package on the system, the path variables are set automatically. If this is not the case, the “command not found” error may be invoked since the system is unable to locate the package even though it is successfully installed onto the system.

Solution: Set Path Variable

Setting the path is very simple in Ubuntu systems. Run the following command to set the Path Variable for the Angular CLI:

$ export PATH="usr/local/lib/node_modules/@angular/cli/bin/ng:$PATH"

The path can be found inside the system directory using the system GUI. Then next, click on the directory/file, then open its properties to find the exact path. An example is shown below:

Add this path to the command shown above. This command will successfully set up the Path for Angular CLI and the “command not found” problem should no longer be prompted.

Conclusion

There are 3 distinct reasons behind the “ng: command not found error”. The Angular CLI is not installed, its Path is not set or it is not linked to the npm. The installation and path problems can be resolved by installing Angular CLI and setting its Path. However, the linkage issue can be resolved by linking the npm to the Angular CLI package to run the ng command. This article has provided the reasons and solutions to resolve the “ng: command not found” problem on your system.