当运行 ng serve 时出现 CLI 错误

9
ng : File C:\Users\Lenovo\AppData\Roaming\npm\ng.ps1 cannot be loaded. The file C:\Users\Lenovo\AppData\Roaming\npm\ng.ps1 is not digitally signed. You cannot run 
this script on the current system. For more information about running scripts and setting execution policy, see about_Execution_Policies at 
https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ ng serve
+ ~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

当执行ng serve时出现错误,请问如何解决?

Rohan,请尝试在标准cmd中使用ng serve。PowerShell与标准cmd非常不同。 - Jacopo Sciampi
标准 cmd 意味着 - Rohan
按下Win徽标键,输入“cmd”,然后按回车键。CMD Shell将会打开。这是标准的CMD。*.ps1文件会在PowerShell中运行,它是一个更复杂的Shell,我认为不受CLI支持。 - Jacopo Sciampi
尝试以下答案:https://dev59.com/KVMH5IYBdhLWcg3w1TyK#58044573 它为我解决了问题。 - Schnikles
7个回答

9

从同一个终端或命令提示符运行以下命令,并重新运行 ng 命令来检查它是否可以在您的计算机上正常工作。

命令为=> Set-ExecutionPolicy RemoteSigned -Scope CurrentUser


5

您需要使用以下命令将当前用户的作用域执行策略设置为RemoteSigned:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

1

分辨率

此问题意味着您的计算机正在阻止运行脚本文件。这些策略的实施发生在 Windows 平台上,包括 Windows 客户端或 Windows 服务器计算机。PowerShell 执行策略如下:

格式和配置文件(.ps1xml)、 模块脚本文件(.psm1)和 PowerShell 配置文件(.ps1)。

解决问题的步骤

要更改 Windows 计算机上的 PowerShell 执行策略,请使用以下命令:

步骤1: 打开 Windows PowerShell 并执行以下命令

Get-ExecutionPolicy -List

步骤2: 一旦确定了您的身份范围和执行策略,请使用相同的内容运行以下命令。

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

准备完成,现在输入ng serve,您就可以开始了。祝编程愉快!


0

从目录C:\ Users \<username>\ AppData \ Roaming \ npm中删除ng.ps1,然后尝试清除C:\ Users \<username>\ AppData \ Roaming \ npm-cache中的npm缓存

对我来说,这很有效,我也遇到了同样的问题。

此修复程序的作者来自THIS帖子。


我还执行了npm update,然后是npm audit fix,但这可能对你来说并不需要,我只是认为我需要这样做。 - CreativeOne

0

你可以尝试使用npm start代替ng serve

如果想要一个永久性的解决方案,你可以查看这个答案


0

对我来说,这个问题是由于VS Code的新安装将powershell作为默认终端而不是CMD引起的。将默认设置更改为CMD并从那里执行可以解决我的问题。


0
  1. 以管理员身份打开Windows Powershell
  2. 运行以下命令:set-executionpolicy remotesigned
  3. 现在您应该能够在Angular和Node.js的Visual Studio Code终端中运行命令了。

来源


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接