为什么 powershell 不运行 Angular 命令?

问题描述 投票:0回答:10

我已经开始学习 Angular,但我注意到每当我执行 Angular 命令时,Windows 中的 powershell 都会出现错误:

ng new new-app

ng serve

这是我得到的错误:

ng : File C:\Users\< username >\AppData\Roaming\npm\ng.ps1 cannot be loaded because 
running scripts is disabled on this system. For more information, see 
about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ ng serve
+ ~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

附注我在 cmd 中尝试了这些命令,它有效。

javascript angular powershell angular-cli
10个回答
884
投票

从目录

ng.ps1
中删除
C:\Users\%username%\AppData\Roaming\npm\
,然后尝试清除位于
C:\Users\%username%\AppData\Roaming\npm-cache\

的 npm 缓存

665
投票

我通过运行以下命令解决了我的问题

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser


41
投票

script1.ps1 无法加载,因为运行脚本被禁用 这个系统。有关详细信息,请参阅 about_Execution_Policies,网址为 http://go.microsoft.com/fwlink/?LinkID=135170

发生此错误的原因是安全措施,未经您批准,脚本不会在您的系统上执行。您可以通过打开具有管理权限的 powershell(在主菜单中搜索 powershell 并从上下文菜单中选择以管理员身份运行)并输入:

来执行此操作
set-executionpolicy remotesigned

12
投票

步骤1

首先,您必须打开命令提示符并运行此命令。

set-ExecutionPolicy RemoteSigned -Scope CurrentUser 

第2步

现在您必须在系统上运行第二个命令。该命令是:

Get-ExecutionPolicy

第3步

要查看他们的策略,您需要在命令提示符中运行以下命令:

Get-ExecutionPolicy -list  

https://www.c-sharpcorner.com/article/how-to-fix-ps1-can-not-be-loaded-because-running-scripts-is-disabled-on-this-sys/


4
投票

发生此错误的原因是安全措施,未经您批准,脚本不会在您的系统上执行。

解决方案:-

  1. 打开 Windows PowerShell

  2. 执行此命令

    set-ExecutionPolicy RemoteSigned -Scope CurrentUser


3
投票

第 1 步:使用以下命令获取您机器的执行策略

Get-ExecutionPolicy -List

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

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

如果上述不起作用,请安装所需的最低节点版本 Angular 并运行以下命令

npm install -g @angular/cli

如果你想处理多个节点版本,请使用 nvm,如下所示;

https://www.freecodecamp.org/news/node-version-manager-nvm-install-guide/


1
投票

发生此错误的原因是安全措施,未经您批准,脚本不会在您的系统上执行。

我还通过执行以下命令解决了我的问题:Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

这个视频帮助我解决了这个问题:https://www.youtube.com/watch?v=a--z7ZV1BqM

你也可以试试这个。希望也能解决您的问题。


0
投票

你可以尝试

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

运行此命令时需要注意的重要事项:

  1. 从项目文件夹运行
    ng
  2. 检查
    ng
    版本
  3. 检查
    angular cli
    是否正确安装,或者您可以通过运行
    npm install -g @angular/cli
  4. 重新安装

0
投票

快速解决方法是从

.bat
文件运行命令

然后它不会运行

ps1
ng

版本

-5
投票

打开Windows powershell,以管理员身份运行并将SetExecution策略设置为Unrestricted然后它就可以工作了。

© www.soinside.com 2019 - 2024. All rights reserved.