Angular: Program ng failed to run No application is associated

961 阅读1分钟

今天,搭建 Angular CLI 框架的时候,遇见了一个奇怪的问题。当我将 Angular CLI 搭建完成以后, 我在 Windows PowerShell 和命令提示符上输入 ng 命令是工作正常的,但在 VSCode PowerShell 上却失败。

问题描述

当我在 VSCode PowerShell 上输入 ng 命令时出现如下错误。

Program 'ng' failed to run: No application is associated with the specified file for this operationAt line:1 char:1

ng
~~.
At line:1 char:1
ng
~~
CategoryInfo : ResourceUnavailable: (:) [], ApplicationFailedException
FullyQualifiedErrorId : NativeCommandFailed

解决方案

在 Windows 计算机上更改 PowerShell 执行策略。步骤如下:

获取执行策略列表

输入如下命令,获取 PowerShell 执行策略列表:

Get-ExecutionPolicy -List

policy-list

设置 CurrentUser 策略

将 CurrentUser 设置成 RemoteSigned 策略,输入如下命令:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

policy-set

以上步骤执行完成后,关闭 VSCode PowerShell,然后重新启动它。最后,重新输入 ng 命令,检查是否成功。

  • 若仍然出现错误,可以尝试关闭所有现有命令提示符和PowerShell 窗口。

  • 若仍然存在问题,可以重新启动电脑。

  • 若问题仍然存在,请检查一下是否设置了 C:\Users\{username}\AppData\Roaming\npm 环境变量,可以参考:Angular: ‘ng’ is not recognized

  • 最后,您也可以尝试重新安装 Angular CLI。

反正各个情况都尝试一遍,确保成功。