the term 'ng' is not recognized as the nam

2019-03-11 16:24发布

问题:

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

回答1:

The first path in the path variable needs to be the NPM path. Opening the Node.js command prompt I found that the ng command worked there. I dug into the shortcut and found that it references a command to ensure the first Path variable is NPM. To Fix:

  1. Right Clicked on My Computer (windows)
  2. Selected Advanced System Settings
  3. Clicked "Environment Variables"
  4. Made the FIRST variable listed %AppData%\npm

Once I did that I was able to close powershell and reopen and all worked.



回答2:

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.



回答3:

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.



回答4:

Installing angular cli globally solved my problem.

npm install -g @angular/cli


回答5:

In the "Environment Variables"

In the "System variables" section

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



回答6:

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.



回答7:

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



回答8:

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



回答9:

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.