How to fix "ng command not found" error for node.js @angular/cli [7 solutions]

If you have landed on this page, it means you are using angular CLI and getting an ng command not found error. In this article, I will explain what are the causes and possible solutions for this error. Once you will follow this article step by step, you will be able to create new projects, develop and maintain your angular applications without further failures.

*** The ng stands and sounds like Angular that's why built-in directives that ships with Angular have this "ng" as the prefix.

If you are getting any of the following errors, follow this post to get it fixed.

• ng is not recognized as an internal or external command

These are the possible reason for getting the ng command not found error -

1. Angular is not installed on your machine
2. NPM is not linked with Angular CLI
3. Variable is not setup for correctly for Angular CLI, NPM and Nodejs
4. Corrupt or obsolete Angular installation
5. Missing alias to ng path
6. Angular CLI link to /usr/loca/bin is missing case of macOS

Below mentioned are the fixes that can be applied, based on your scenario and operating system.

The first step is to check whether you have @angular/cli is installed or not. Please make a note the older version "angular-CLI" has been depreciated. In case you have the old version, the suggestion is to install the latest angular version.

1.1 Check whether @angular/cli is installed

You must have npm and node.js installed correctly in your system as a pre-requisite before @angular/cli installation. The recommendation is to have the latest or compatible node.js and npm versions.

Run the following command to check @angular/cli package installation on your system

npm ls --global --depth 0

In this image, only nodemon is installed, @angular/cli module is not installed.

If the angular cli package is installed, then you can move to the next solution. Else run the following command to install angular cli on your system. Follow the "npm command not found" post, in case you have issues in npm.

How to install latest angular cli globally

• To install Angular cli on your system, follow these commands -
# npm install -g @angular/cli                                \\in case root user

or

$sudo npm install -g @angular/cli Side Note** - You will get warnings, in case angular cli or any existing module is not compatible with exiting node.js and npm and requires updates. The recommendation is to have the latest npm and node js installed. For example, in this image, to install the latest @angular/cli@13.3.0, I would need minimum node version 12.2.0 and npm version 7.5.6. and my existing version is showing npm: 7.5.2 and am getting warnings of the unsupported engine. So I must upgrade npm to the latest that's version 8.0.0 to avoid any future issues. Close and reopen your terminal. • Then Check ng version ng version • Create a workspace and new project for your application ng new my-app If you are pretty new to Angular, then I will recommend reading these helpful Angular docs. Fix-2 Link NPM with angular/cli to fix ng command not found problem If you follow the correct approach during angular cli installation as shown in Fix-1, then you don't need to create any npm and angular links. But the fact is sometimes ng doesn't know about npm and you need to link it with angular/cli manually. So run the following command to link npm with angular/cli. • Most of the ng command not found errors are fixed using the following npm link command npm link @angular/cli or sudo npm link @angular/cli Fix-3 Setup the variable correctly for your operating system If you have followed all the required steps to install NPM, Nodejs and angular/cli correctly. You won't need to set up the path variable, it will automatically be setup during installation. But in case it doesn't then keep following further steps. • To setup path variable in Unix, Linux or macOS - export PATH="usr/local/lib/node_modules/@angular/cli/bin/ng:$PATH"

to make it persistent, add the above command in the ~/.profile, ~/.bashrc or ~/.zshrc file based on the shell you are using.

In case of multiple projects and using different cli versions. The following command will help you to pick the local version (relative to the current directory), as long as you run the ng command from within the root directory of your Angular (sub)project.

Fix-6 Setup alias for ng command

Many times setting up an alias for ng command works and helps to fix ng command not found error.

• Remove existing or old alias of ng, in case any
unalias ng
alias ng="/Users/<user_name>/.npm-global/bin/ng"
• Run ng -v command to check version.

Fix-7 Update or reinstall Corrupt or old Angular/cli installation to resolve "ng command not found error"

You may or may not know, that the angular-cli version has been depreciated now, my suggestion is to update it with the latest version @angular/cli.

To uninstall the old angular-cli version and install the latest one, follow these steps -

• Uninstall old version of angular-cli or @angular/cli and clean cache.
npm uninstall -g angular-cli

npm uninstall -g @angular/cli

npm cache clean
• Then you need to delete C:\Users\%YOU%\AppData\Roaming\npm\node_modules\@angular folder in case of Windows 10.
• For Linux or macOS users look for node_modules/@angular/cli/bin/ng folder and delete it.
• Reboot your system once is recommended. then run following command to install angular/cli.
npm install -g @angular/cli@latest
• Check ng version
ng -v

Conclusion

The error "ng command not found" is a ghost and can appear any time, even if you are done everything right in the first place during installation. So equip yourself with the above troubleshooting steps and fight with this ghost.

I hope one or another solution will definitely work for you. Please let me know, which solution fixed your issue via your comments.