我正在尝试在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)如所建议here和also here
我尝试访问并以administrator
的身份运行,如下图所示:
但是我在 shell 上看到的唯一东西是下面的内容(但是 shell 立即消失了,我只有时间来打印屏幕)
3)我对问题进行了深入研究,发现了一些其他见解,例如this one和this additional source。但是,该帖子指出的唯一一件事是再次使用了我在上面的第1点中尝试过的Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
。
4)经过更多的研究,我发现this one是可惜的,但是我没有使用任何有用的信息。
代码
如果有用的话,下面是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
问题的想法,任何其他方向的见解或指导都将对解决此问题有用。
最佳答案
您必须将执行策略设置为不受限制的而非RemoteSigned
。根据Set-ExecutionPolicy:
由于所有PowerShell脚本中有99%是未签名的,因此您有99%的时间将无法在此策略下运行它们。
。
Set-ExecutionPolicy -ExecutionPolicy Unrestricted