ng:术语 'ng' 未被识别为 cmdlet 的名称,函数 Visual Studio 代码终端

ng : The term 'ng' is not recognized as the name of a cmdlet, function Visual Studio Code Terminal

在 VS Code 中,当我 运行 命令 ng build 时,出现错误

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

我在Path

中添加了这些环境变量
%USERPROFILE%\AppData\Local\Microsoft\WindowsApps;
C:\Users\Avind\AppData\Local\Programs\Microsoft VS Code\bin;
C:\Users\Avind\AppData\Roaming\npm;

如何解决这个问题?

注意:我在 C:\Program Files\nodejsnode.js。从命令提示符 ng new project 不工作但从 node.js 命令工作。

编辑 1:从 VS Code 终端执行 npm install -g @angular/cli@latest 后得到低于 msg 和 ng cmd 不工作

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

如此处 Angular CLI,确保安装了 Angular CLI
您可以从系统的任何位置运行执行此命令

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

应该打印类似的东西

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


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)

您需要安装最新的 angular cli 才能使 ng 命令工作,您可以从命令终端 运行 或 visual studio 代码终端

npm install -g @angular/cli@latest

如果仍然无法正常工作,请从 program and feature 中卸载并重新安装 node.js

我遇到了同样的问题。我在新选项卡中重新打开 CMD 并输入如下内容:

ng g c componentname

然后按 Enter,对我有用。

这对我有用,上面的尝试在我的情况下不起作用。

Run this command -> Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

参考:

http://www.techtutorhub.com/article/Getting-ng-File-AppData-Roaming-npm-ng-ps1-cannot-be-loaded-The-file-npm-ng-ps1-is-not-digitally-signed-Angular-Error-when-running-commands/62

方法一:

第 1 步:运行 命令“Set-ExecutionPolicy RemoteSigned -Scope CurrentUser”

当你运行这个命令时,你可以看到你的系统已经为当前用户设置了所有策略为remote。完成此过程需要几秒钟。

第 2 步:在此处输入图像描述 Get-ExecutionPolicy -list 当您 运行 此命令时,一些策略会显示在您的监视器屏幕上 验证当前用户的策略是否为 RemoteSigned。 ->如果是,现在应该可以了。 如果否,请尝试方法 2,如下所示:

方法二: 在路径 C:\Users%username%\AppData\Roaming\npm\ 中找到文件“ng.ps1”,然后 从目录中删除它。 如果您不知道文件扩展名,请右键单击该文件 -> 转到属性并检查文件扩展名。

注意:-请不要删除任何其他文件。确保只删除 ng.ps1 文件。

这个问题可能是因为没有设置环境变量。 这对我有用:

运行:

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

安装完成后进入高级系统配置->环境变量->路径->编辑

如果此 Angular 变量不在路径中,您添加: “C:\Users\youruser\AppData\Roaming\npm”

关闭终端,打开它并再次运行命令

我在使用 Powershell 时遇到了类似的错误,我使用以下命令解决了它:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

您需要安装最新的 angular cli 才能使 ng 命令工作,您可以从命令终端或 visual studio 代码终端 运行 安装它。

#> npm install -g @angular/cli@latest

如果仍然无法正常工作,请从程序和功能中卸载并重新安装 node.js。

我通过 运行 这三个命令解决了这个问题:-

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

成功了。

试试常见的
npm install -g @angular/cli@latest

然后使用以下内容:- 1. 打开 Windows PowerShell 2. 运行

    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

4 步修复:

  1. 卸载node.js和
  2. 转到网站:https://nodejs.org/en/
  3. 使用最新的 LTS 重新安装(见下图)
  1. 最后,再次尝试上面接受的答案。

LTS - 代表长期支持