[node.js] The term 'ng' is not recognized as the name of a cmdlet

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

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

I was able to install the 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

This question is related to node.js angular powershell npm angular-cli

The answer is


If you do not have access to Environmental variable (e.g. office machines).

You can try to run command like this:

npm run <your angular command>

it works as well, just need to add npm run before ng command

Example -> npm run ng g c shop/cart

enter image description here


I used the following:

npm run ng serve

It worked for me without need to set Environment variables.

I had to install a typescript after it:

npm install typescript@">=3.4 <3.6"

Open Edit the system environment variables

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

Run Visual Code as Administrator

It works for me!


Even though the correct answers have been given. But 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.

So for all of you who face the same issue as me 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


For VSCode Terminal First open cmd and install angular-cli as global

npm install -g @angular/cli

Then update your Environment Variables following this step

  1. win + s // this will open 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 VSCode and it will work as it will normally do.


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

Version 12 and 13 are too new, so I had to download 11.15 https://nodejs.org/download/release/v11.15.0/


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


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 VS Code as admin.


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

Then give "ng serve" command.

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

It works for me.


Reinstalling NodeJs is also an option. It will set PATH variable automatically.


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 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 cache and repeats the above steps.


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


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


First setup Node.js, then go to your project folder with the command prompt such as D:\project and then run this command:

npm install -g @angular/cli

Now run ng command. This work for me.


exit everything and run npm i -g @angular/cli in command prompt , and build your angular app there itself(do not build on powershell)


You just need to close the visual studio code and restart again. But to get the ng command work in vs 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.


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.


Based on the above 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 Variables -> Path (For which Admin 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.

If your project name contain '-'. Remove it and try. This can cause problem in running 'ng'.


Selected Advanced System Settings enter image description here Clicked "Environment Variables" enter image description here Under "Path" variable, made the FIRST value listed %AppData%\npm enter image description here I did that.Close powershell and reopen. all worked


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

PS: I have installed angular globally.


I resolved by following the below steps

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


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"                                             

1)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 error 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-->Properties-->Advanced system setting -->Environment Variable -->add a path below "User variable"(if 'Path' not there) ==>C:\Users\brijeshray\AppData\Roaming\npm -->save it and restart visual codeenter image description here


In the "Environment Variables"

In the "System variables" section

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


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 following command to resolve, npm install -g @angular/cli


Run Powershell or command prompt not as administrator.


Installing angular cli globally solved my problem.

npm install -g @angular/cli

Fix: Running scripts is disabled on this system

Open Powershell

    Set-ExecutionPolicy RemoteSigned
    A

(A: YES TO ALL)

Done!


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

npm install -g @angular/cli@latest

Changing the policy to Unrestricted worked for me:

Set-ExecutionPolicy Unrestricted -Scope CurrentUser

Examples related to node.js

Hide Signs that Meteor.js was Used Querying date field in MongoDB with Mongoose SyntaxError: Cannot use import statement outside a module Server Discovery And Monitoring engine is deprecated How to fix ReferenceError: primordials is not defined in node UnhandledPromiseRejectionWarning: This error originated either by throwing inside of an async function without a catch block dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac internal/modules/cjs/loader.js:582 throw err DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean`

Examples related to angular

error NG6002: Appears in the NgModule.imports of AppModule, but could not be resolved to an NgModule class error TS1086: An accessor cannot be declared in an ambient context in Angular 9 TS1086: An accessor cannot be declared in ambient context @angular/material/index.d.ts' is not a module Why powershell does not run Angular commands? error: This is probably not a problem with npm. There is likely additional logging output above Angular @ViewChild() error: Expected 2 arguments, but got 1 Schema validation failed with the following errors: Data path ".builders['app-shell']" should have required property 'class' Access blocked by CORS policy: Response to preflight request doesn't pass access control check origin 'http://localhost:4200' has been blocked by CORS policy in Angular7

Examples related to powershell

Why powershell does not run Angular commands? How do I install the Nuget provider for PowerShell on a unconnected machine so I can install a nuget package from the PS command line? How to print environment variables to the console in PowerShell? Check if a string is not NULL or EMPTY The term 'ng' is not recognized as the name of a cmdlet VSCode Change Default Terminal 'Connect-MsolService' is not recognized as the name of a cmdlet Powershell Invoke-WebRequest Fails with SSL/TLS Secure Channel Install-Module : The term 'Install-Module' is not recognized as the name of a cmdlet Change directory in PowerShell

Examples related to npm

What does 'x packages are looking for funding' mean when running `npm install`? error: This is probably not a problem with npm. There is likely additional logging output above Module not found: Error: Can't resolve 'core-js/es6' Browserslist: caniuse-lite is outdated. Please run next command `npm update caniuse-lite browserslist` ERROR in The Angular Compiler requires TypeScript >=3.1.1 and <3.2.0 but 3.2.1 was found instead DeprecationWarning: Buffer() is deprecated due to security and usability issues when I move my script to another server Please run `npm cache clean` What exactly is the 'react-scripts start' command? On npm install: Unhandled rejection Error: EACCES: permission denied Difference between npx and npm?

Examples related to angular-cli

Why powershell does not run Angular commands? ERROR in The Angular Compiler requires TypeScript >=3.1.1 and <3.2.0 but 3.2.1 was found instead Angular CLI Error: The serve command requires to be run in an Angular project, but a project definition could not be found Could not find module "@angular-devkit/build-angular" How to remove package using Angular CLI? How to set environment via `ng serve` in Angular 6 Error: Local workspace file ('angular.json') could not be found How do I deal with installing peer dependencies in Angular CLI? How to iterate using ngFor loop Map containing key as string and values as map iteration how to format date in Component of angular 5