electron - node.js - ng : 无法加载文件路径\\ng.ps1,因为在此系统上禁用了运行脚本

新手上路,请多包涵

我正在尝试在 Windows 10 中的 Visual Studio Code 上编译一个项目,我的设置如下:

  1. npm 版本 6.12

  2. Node.js 版本 12.13

  3. Angular CLI: 8.3.19

我遇到的问题是,一旦我尝试运行 ng serve 我收到以下输入: ng : File C:\Users\name\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.

到目前为止我尝试了什么

1) 我去了网站上的建议,发现 Set-ExecutionPolicy -ExecutionPolicy RemoteSigned 应该是我必须在shell中输入的命令。因此我所做的是:

PS C:\Users\raggi\OneDrive\Desktop\pitalkdashboard> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

但这导致了以下错误:

Set-ExecutionPolicy : Access to the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell is denied. To change the execution policy for the default (LocalMachine) scope, start Windows PowerShell with the "Run as administrator" option. To change the execution policy for the current user, run "Set-ExecutionPolicy -Scope CurrentUser".

这正是我所做的,所以按顺序:

PS C:\Users\raggi\OneDrive\Desktop\pitalkdashboard> Set-ExecutionPolicy -Scope CurrentUser

下面是 Shell 提示的,我只需要插入 RemoteSigned

cmdlet Set-ExecutionPolicy at command pipeline position 1

Supply values for the following parameters:

ExecutionPolicy: RemoteSigned

之后,我尝试 ng serve 启动整个项目,但收到不同的错误,我将其放在打印屏幕下方,因为它太长了:

错误

对这条路线没有更多的想法。

2) 这里这里的 建议

我尝试以 administrator 访问并运行,如下面的打印屏幕所示:

行政

但我在外壳上看到的唯一东西是下面的(但是外壳马上消失了,我只有时间来打印屏幕)

外壳2

3) 我深入研究了这个问题,发现了一些额外的见解,比如 这个这个额外的来源。然而,该帖子唯一指出的是,再次使用 Set-ExecutionPolicy -ExecutionPolicy RemoteSigned 我在上面的第 1) 点尝试过。

4) 经过更多研究,我遇到了 这个, 但不幸的是我没有使用任何有用的信息。

编码

以下是我的 package.json 如果有用的话:

 {
  "name": "pi-talk-dashboard",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@agm/core": "^1.0.0-beta.5",
    "@angular/animations": "^6.0.3",
    "@angular/common": "^6.0.3",
    "@angular/compiler": "^6.0.3",
    "@angular/core": "^6.0.3",
    "@angular/forms": "^6.0.3",
    "@angular/http": "^6.0.3",
    "@angular/platform-browser": "^6.0.3",
    "@angular/platform-browser-dynamic": "^6.0.3",
    "@angular/router": "^6.0.3",
    "chart.js": "^2.7.3",
    "core-js": "^2.5.4",
    "electron": "^7.1.0",
    "express": "^4.17.1",
    "moment": "^2.22.2",
    "ng2-charts": "^1.6.0",
    "rxjs": "^6.0.0",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.803.17",
    "@angular/cli": "~6.0.8",
    "@angular/compiler-cli": "^6.0.3",
    "@angular/language-service": "^6.0.3",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "^5.4.1",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1",
    "typescript": "~2.7.2"
  }
}

编辑

以管理员身份运行 -ExecutionPolicy Unrestricted 后,以下脚本在 notepad 中打开,但我不确定它的用途。我附上了打开的文件的打印屏幕和代码:

行政

代码如下:

 #!/usr/bin/env pwsh
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent

$exe=""
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
  # Fix case when both the Windows and Linux builds of Node
  # are installed in the same directory
  $exe=".exe"
}
$ret=0
if (Test-Path "$basedir/node$exe") {
  & "$basedir/node$exe"  "$basedir/node_modules/@angular/cli/bin/ng" $args
  $ret=$LASTEXITCODE
} else {
  & "node$exe"  "$basedir/node_modules/@angular/cli/bin/ng" $args
  $ret=$LASTEXITCODE
}
exit $ret

编辑 2

在 PowerShell 上运行后 -ExecutionPolicy Unrestricted 没有任何变化。但是出现了一个错误。 Data path ".builders['app-shell']" should have required property 'class'. 见下面的打印屏幕

电源外壳

我对如何运行这个项目以及如何解决这个 ng serve 问题没有任何想法,任何其他方向的见解或指导都将有助于解决这个问题。

原文由 Emanuele 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.3k
2 个回答

您必须将执行策略设置为 Unrestricted 而不是 RemoteSigned 。根据 Set-ExecutionPolicy

远程签名。要求从 Internet 下载的所有脚本和配置文件都由受信任的发布者签名。 Windows 服务器计算机的默认执行策略。

由于 99% 的 PowerShell 脚本未签名,因此 99% 的时间您将无法在此策略下运行它们。

  1. 以管理员身份打开 PowerShell。
  2. 跑:

.

 Set-ExecutionPolicy -ExecutionPolicy Unrestricted

原文由 HAL9256 发布,翻译遵循 CC BY-SA 4.0 许可协议

  • 问题

我遇到了同样的问题,经过一些研究,我发现只需绕过限制并通过 ng 服务器启动您的应用程序。

  • 在终端中,您可以输入以下内容:

PS C:\Users\\ClientApp> Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser

PS C:\Users\\ClientApp> 服务

  • 重新运行您的应用程序:

http://localhost:4200/

原文由 Ashutosh Soni 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题