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

Ema*_*ele 22 powershell node.js electron npm-start angular

我正在尝试在Windows 10in上编译一个项目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)我对这个问题进行了更多的挖掘,并发现了一些额外的见解,例如this onethis additional source。然而,该帖子唯一指出的是,再次使用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"
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑

以管理员身份运行 -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
Run Code Online (Sandbox Code Playgroud)

编辑 2

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

电源外壳

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

HAL*_*256 82

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

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

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

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

.

Set-ExecutionPolicy -ExecutionPolicy Unrestricted
Run Code Online (Sandbox Code Playgroud)


小智 11

  • 问题

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

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

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

PS C:\Users\\\ClientApp> ng serve

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

http://本地主机:4200/


小智 9

以管理员身份打开 Powershell 并执行以下命令:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted
Run Code Online (Sandbox Code Playgroud)


小智 8

在您的项目文件夹中运行此命令

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Run Code Online (Sandbox Code Playgroud)

祝你好运


小智 7

尝试使用“npx ng”而不是“ng”。如果我尝试使用任何“ng”命令,包括“ng new”,我会得到相同的权限错误。使用这个 npm 包运行器为我解决了这个问题。


归档时间:

查看次数:

59462 次

最近记录:

4 年,3 月 前