11

In Visual Studio Code, when I am running command ng build, I am getting this error:

PS D:\Dashboard\Test1> ng build
ng : The term 'ng' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if
a path was included, verify that the path is correct and try again.
At line:1 char:1
+ ng build
+ ~~
    + CategoryInfo          : ObjectNotFound: (ng:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

I have added these environment variables in Path:

%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;
C:\Users\Avind\AppData\Local\Programs\Microsoft VS Code\bin;
C:\Users\Avind\AppData\Roaming\npm;

How can I get rid of this issue?

Note: I have node.js in C:\Program Files\nodejs. From the command prompt, ng new project is not working, but it is working from a node.js command.

After executing npm install -g @angular/cli@latest from the Visual Studio Code terminal, I am getting the below message and the ng command is not working:

PS D:\Path\Test1> npm install -g @angular/cli@latest
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
C:\Users\Avind\AppData\Roaming\npm\ng -> C:\Users\Avind\AppData\Roaming\npm\node_modules\@angular\cli\bin\ng

> @angular/cli@9.1.9 postinstall C:\Users\AR20095474\AppData\Roaming\npm\node_modules\@angular\cli
> node ./bin/postinstall/script.js

+ @angular/cli@9.1.9
updated 1 package in 53.477s
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
R15
  • 13,982
  • 14
  • 97
  • 173

10 Answers10

21

You need to install the latest Angular CLI to make the 'ng' command work. You can run it from the command terminal or Visual Studio Code terminal:

npm install -g @angular/cli@latest

If it is still not working, uninstall and reinstall Node.js from Programs and Features.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
8

As here Angular CLI, make sure you installed Angular CLI.
You can run this command from anywhere on your system.

npm install -g @angular/cli
ng --version

It should print something like this:

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/


Angular CLI: 13.1.2
Node: 16.13.1
Package Manager: npm 8.1.2
OS: win32 x64

Angular:
...

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.1301.2 (cli-only)
@angular-devkit/core         13.1.2 (cli-only)
@angular-devkit/schematics   13.1.2 (cli-only)
@schematics/angular          13.1.2 (cli-only)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CorrM
  • 498
  • 6
  • 18
5

This worked for me. Trying previous answers did not work in my case.

Run this command

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

Reference:

ng : File \AppData\Roaming\npm\ng.ps1 cannot be loaded. The file npm\ng.ps1 is not digitally signed Angular Error when running commands

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pooja Burande
  • 91
  • 1
  • 5
3

I received a similar error while working with PowerShell, and I used the following command to resolve it:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

Method 1:

Step 1: Run the command "Set-ExecutionPolicy RemoteSigned -Scope CurrentUser"

When you run this command, you can see that your system has set all policies for the current user as remotely. It will take few seconds to complete this process.

Step 2: Enter image description here

Get-ExecutionPolicy -list

When you run this command, a few policies are shown on your monitor screen.

Verify if the policy for the current user is RemoteSigned. If yes, this it should work now. If no, please try method 2 as below:

Method 2:

Find the file "ng.ps1" in path C:\Users%username%\AppData\Roaming\npm\ and remove it from the directory.

If you don't know the file extension, then right-click on the file -> go to Properties and check the extension of file.

Note: Please do not remove any other file. Make sure you are removing the ng.ps1 file only.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Removing ng.ps1 seems to force the file to be recreated, this time with the proper signature. That seems a lot safer than fiddling with policy. – Billdr Oct 03 '21 at 14:42
2

Try this command:

npm install -g @angular/cli@latest

Then use the following:

  1. Open Windows PowerShell

  2. Run

    Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
    Get-ExecutionPolicy
    

    and you get

    RemoteSigned
    
  3. Run

    Get-ExecutionPolicy -list
    

    You get

    Scope          ExecutionPolicy
    -----          ---------------
    MachinePolicy  Undefined
    UserPolicy     Undefined
    Process        Undefined
    CurrentUser    RemoteSigned
    LocalMachine   Undefined
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abhay Kumar Upadhyay
  • 2,117
  • 1
  • 7
  • 12
0

I had the same issue. I reopened CMD in a new tab and typed the same thing like this:

ng g c componentname

and hit Enter, it worked for me.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
0

This issue may be due to the Path environment variable not being set.

This worked for me:

Run:

npm uninstall -g angular-cli
npm uninstall --save-dev angular-cli
npm install -g @angular/cli

After installing, you enter Advanced System ConfigurationEnvironment VariablesPathEdit.

If this Angular variable is not in the path, you add: "C:\Users\youruser\AppData\Roaming\npm"

Close the terminal, open it and run the command again.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

I solved this problem by just running these three commands:

npm uninstall -g @angular/cli angular-cli
npm cache clean or npm cache clean --force
npm install -g @angular/cli@latest

It worked.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Abhay Kumar Upadhyay
  • 2,117
  • 1
  • 7
  • 12
0

Four steps to fix:

  1. Uninstall Node.js and

  2. Go to website: https://nodejs.org/en/

  3. Reinstall with the latest LTS[1] (see image below)

    Enter image description here

  4. Finally, try the accepted answer above again.

[1]: LTS stands for long term support

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RetroCoder
  • 2,597
  • 10
  • 52
  • 81