【发布时间】:2020-03-11 18:58:37
【问题描述】:
我正在尝试在Visual Studio Code 中的Windows 10 上编译一个项目,我的设置如下:
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 启动整个项目,但收到不同的错误,我将其放在打印屏幕下方,因为它太长了:
对这条路线没有更多的想法。
我尝试以administrator 的身份访问和运行,如下面的打印屏幕所示:
但我在 shell 上看到的唯一内容如下(但是 shell 立即消失了,我只来得及打印屏幕)
3) 我深入研究了这个问题,发现了一些额外的见解,例如this one 和this additional source。然而,该帖子唯一指出的是,再次使用Set-ExecutionPolicy -ExecutionPolicy RemoteSigned,我在上面的第 1) 点尝试过。
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 问题没有任何想法,任何其他方向的见解或指导都将有助于解决这个问题。
【问题讨论】:
-
这应该和我最近发现有问题的npm的一个特性有关:github.com/npm/cli/issues/470>。快速而肮脏的修复方法是在命令之前添加
cmd(例如cmd ng serve)或在命令之后添加.cmd(例如ng.cmd serve),甚至只是删除%appdata%\npm中的所有ps1文件来强制powershell 使用 cmd 文件。
标签: node.js angular powershell electron npm-start