Message "the term 'ng' is not recognized as the name of a cmdlet"

Viewed 460839

Today, while working through some basic AngularJS introduction, I ran into a problem.

I opened PowerShell to get going on the project. NPM worked.

I was able to install Angular using:

npm install -g @angular/cli

Anytime I tried to run ng, I would get:

the term 'ng' is not recognized as the name of a cmdlet

40 Answers

First set up Node.js, and then run this command to install Angular globally:

npm install -g @angular/cli

Now run the ng command. This worked for me.

I used the following:

npm run ng serve

It worked for me without need to set environment variables.

I had to install the TypeScript package after it:

npm install typescript@">=3.4 <3.6"

In the "Environment Variables"

In the "System variables" section

In the "Path" variable and before "C:\Program Files (x86)\nodejs\" add => "%AppData%\npm"

This solution worked for me:

Add a path to your environment Variable

C:\Users\YourPcName\AppData\Roaming\npm

As well as your bin folder of the Angular file (present their itself):

C:\Users\YoutPcName\AppData\Roaming\npm\node_modules\angular-cli\bin

And then run

ng -v

It will pop up the Angular CLI GUI in your Command prompt.

Note after running npm i -g @angular/cli, do restart your command prompt and check if it works. Otherwise, clean the cache and repeat the above steps.

Changing the policy to Unrestricted worked for me:

Set-ExecutionPolicy Unrestricted -Scope CurrentUser

Installing Angular CLI globally solved my problem.

npm install -g @angular/cli

If you do not have access to environment variables (e.g., office machines), you can try to run a command like this:

npm run <your Angular command>

It works as well. You just need to add npm run before ng command.

Example:

npm run ng g c shop/cart

Enter image description here

I was getting this error in Visual Studio Code while doing ng-build. Running the below command in cmd fixed my issue:

npm install -g @angular/cli@latest

For the Visual Studio Code Terminal

First open cmd and install angular-cli as global

npm install -g @angular/cli

Then update your environment variables following these steps:

  1. Win + S. This will open a search box
  2. Type Edit Environment Variables
  3. Open Environment Variables
  4. Add %AppData%\npm inside PATH
  5. Click OK and Close.

Now you can restart your Visual Studio Code and it will work as it will normally do.

Selected Advanced System Settings

Enter image description here

Clicked "Environment Variables"

Enter image description here

Under "Path" variable, made the first value listed to be %AppData%\npm

Enter image description here

I did that. Close PowerShell and reopen. All worked.

If your project name contains '-', remove it and try.

This can cause problems in running 'ng'.

  1. I right-clicked on My Computer (Windows)
  2. Selected Advanced System Settings
  3. Clicked "Environment Variables"
  4. Under "Path" variable, made the first value listed %AppData%\npm

Initially:

C:\Program Files\Microsoft MPI\Bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0;C:\Program Files\TortoiseSVN\bin;

After path adding:

C:\Program Files\Microsoft MPI\Bin;%AppData%\npm;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0;C:\Program Files\TortoiseSVN\bin;

One more thing you can try, if the error is still coming, as below

  1. Go to project location via command prompt:C:\Users\brijeshray\ParentChild>

  2. Reinstall or update existing angular as:

     npm install -g@angular/cli@latest
    
  3. Go to computer or PC → PropertiesAdvanced system settingEnvironment Variable → add a path below "User variable" (if 'Path' not there) → C:\Users\brijeshray\AppData\Roaming\npm → save it and restart Visual Studio Code

    Enter image description here

Open Edit the system environment variables.

In the "Path" and "PS Module Path" variable, add "%AppData%\npm"

Run Visual Studio Code as Administrator.

It works for me!

You just need to close Visual Studio Code and restart again. But to get the ng command to work in Visual Studio Code, you need to first compile the project with cmd in administrator mode.

I was also facing the same problem. But this method resolved it.

I resolved it by following the below steps:

  1. Right click on command prompt
  2. Run as administrator
  3. Type npm install -g @angular/cli

Fix: Running scripts is disabled on this system

Open PowerShell

Set-ExecutionPolicy RemoteSigned
A

(A: YES TO ALL)

Done!

All answers are about how to fix it, but the best is to download Node.js and let the installer add to PATH variable.

Version 12 and 13 are too new, so I had to download 11.15.

Instead of using the "ng serve" command in the Visual Studio code terminal, open the Angular app path in the command prompt (Run as Administrator).

Then issue "ng serve" command.

Then open a browser and go to the http://localhost:4200/.

It works for me.

You can also make sure you run the Command Prompt - or whatever terminal you use - As Administrator. I am using Visual Studio Code and the ng serve command gives me that exact error when not running Visual Studio Code as an administrator.

You should update Node.js to the latest version. Otherwise uninstall Node.js and install it again.

This PowerShell script worked to add the correct setting to my environment variable "Path" (as a per-user setting). It adds: %AppData%\npm—and then restart the command line that uses "ng".

$existingPath = [System.Environment]::GetEnvironmentVariable("Path", "User")
Write-Host "existing PATH variable is $existingPath"
$newPath = "%AppData%\npm;$existingPath"
Write-Host "new      PATH will be     $newPath"

# Update here
[System.Environment]::SetEnvironmentVariable("Path", $newPath, "User")

$finalPath = [System.Environment]::GetEnvironmentVariable("Path", "User")
Write-Host "final    PATH variable is $finalPath"

The problem is not the install of the NPM nor the path!

If you want to use the "ng" command, you need to install the angular-cli by running the following command

npm install -g @angular/cli

https://cli.angular.io/

Also you can run the following command to resolve it:

npm install -g @angular/cli

Exit everything and run npm i -g @angular/cli in a command prompt

And build your Angular application there itself (do not build on PowerShell).

I was using npm (5.5.1). Updating it to the latest version solved my problem.

Run PowerShell or command prompt not as administrator.

After changing the path you have to restart PowerShell. You do not need to restart your computer.

Based on the previous answers, here is the consolidation.

  1. Run the command npm install -g @angular/cli@project_version

  2. Add the below paths to your environment variables → System VariablesPath (for which administrator access is required).

    C:\Users\YourPcAccountName\AppData\Roaming\npm
    C:\Users\YoutPcAccountName\AppData\Roaming\npm\node_modules\angular-cli\bin
    

    Make sure the first value listed as %AppData%\npm

  3. Reopen your command prompt from your project folder and run ng serve.

I ran the 'ng serve' command in the command prompt. It compiled the project successfully. Then whatever changes are saved in Visual Studio Code, are automatically refreshed in the browser.

PS: I have installed Angular globally.

Please also make sure that the node_modules folder also exists there in the project directory. If it is not there you will get a similar issue. So please run npm install as well.

Even though the correct answers have been given, all of these didn't work for me because:

  • My username didn't have the Administrator privileges and I couldn't update the environment variable like suggested in the answers.

Here is what I did:

Instead of ng serve, I copy-pasted the complete location path of ng like the following and it worked.

So the ng serve command became:

C:\Users\MyUserName\AppData\Roaming\npm\ng.cmd serve

Reinstalling Node.js is also an option. It will set the PATH variable automatically.

In the "Environment Variables"

In the "System variables" section

In the "Path" variable and before "C:\Program Files (x86)\nodejs" add => "%AppData%\npm"

And also do:

npm install

Then restart your Visual Studio Code. This worked for me.

It is helped for me (for Windows):

  1. Delete folders:

    C:\Users\**YourName**\AppData\Roaming\npm
    C:\Users\**YourName**\AppData\Roaming\npm-cache
    
  2. Install Node.js from https://nodejs.org/en/download/

  3. In the terminal:

    npm install -g @angular/cli
    
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

This worked for me.

It can be done without updating environment variables
create new project:

npx @angular/cli@latest new project-name

now you can use ng commands inside that project

Remove the @ng_module directory and run ng init. this command basically installs all the packages and modules required for the project.

ps: don't remove the package.json and angular.json from the project.

vs code and native powershell

powershell > run in admin follow same steps

run in vs code admin

npm install -g @angular/cli

close vs code

run vs code not admin

ng will work

Related