npm安装-g @angular/cli 失败,出现语法错误

5

我有一个在CentOS 7容器上运行的Jenkins作业。 一旦容器启动,Jenkins作业会运行一个脚本,执行以下操作:

 npm install -g @angular/cli

这个方法已经使用了一年,但今天开始出现故障:

 /usr/bin/ng -> /usr/lib/node_modules/@angular/cli/bin/ng
 > @angular/cli@8.0.4 postinstall /usr/lib/node_modules/@angular/cli
 > node ./bin/postinstall/script.js
 /usr/lib/node_modules/@angular/cli/bin/postinstall/analytics-prompt.js:8
 (async () => {
 ^
 SyntaxError: Unexpected token (
 at createScript (vm.js:56:10)
 at Object.runInThisContext (vm.js:97:10)
 at Module._compile (module.js:549:28)
 at Object.Module._extensions..js (module.js:586:10)
 at Module.load (module.js:494:32)
 at tryModuleLoad (module.js:453:12)
 at Function.Module._load (module.js:445:3)
 at Module.require (module.js:504:17)
 at require (internal/module.js:20:19)
 at Object.<anonymous> (/usr/lib/node_modules/@angular/cli/bin/postinstall/script.js:5:1)
 npm ERR! Linux 3.10.0-957.1.3.el7.x86_64
 npm ERR! argv "/usr/bin/node" "/usr/bin/npm" "install" "-g" "@angular/cli"
 npm ERR! node v6.16.0
 npm ERR! npm  v3.10.10
 npm ERR! code ELIFECYCLE
 npm ERR! @angular/cli@8.0.4 postinstall: `node ./bin/postinstall/script.js`
 npm ERR! Exit status 1
 npm ERR!
 npm ERR! Failed at the @angular/cli@8.0.4 postinstall script 'node ./bin/postinstall/script.js'.
 npm ERR! Make sure you have the latest version of node.js and npm installed.
 npm ERR! If you do, this is most likely a problem with the @angular/cli package,
 npm ERR! not with npm itself.
 npm ERR! Tell the author that this fails on your system:
 npm ERR!     node ./bin/postinstall/script.js
 npm ERR! You can get information on how to open an issue for this project with:
 npm ERR!     npm bugs @angular/cli
 npm ERR! Or if that isn't available, you can get their info via:
 npm ERR!     npm owner ls @angular/cli
 npm ERR! There is likely additional logging output above.
 npm ERR! Please include the following file with any support request:
 npm ERR!     /app/capman-fe/npm-debug.log

我的packages.json文件内容如下:

{
  "name": "pman-fe",
  "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": {
... omitted for brevity ...
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.6.8",
    "@angular/cli": "^6.0.8",
    "@angular/compiler-cli": "^6.0.3",
    "@angular/language-service": "^6.0.3",
    "@types/d3": "^5.7.1",
    "@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"
  }
}

这是始终要求使用库的最新版本的结果。 - Mike Doe
好的,那我该怎么修复它呢? - Red Cricket
1
我遇到了这个问题,我只是想尝试重新安装Angular CLI。他们的软件包出了问题吗? - David Price
@DavidPrice 我是通过 yumrpm 安装我的 npm,你是使用 Redhat 还是 Centos?如果是的话,请尝试安装 yum 存储库并使用那里的 rpm - Red Cricket
我在使用Windows系统。很抱歉,我看到你在使用Linux系统。也许我的node/npm有问题?我从未在这台机器上安装过Angular CLI。 - David Price
3个回答

3
我成功解决了这个错误。问题在于当我安装npm时,我只执行了以下命令:yum -y install npm。我猜测容器需要使用的yum仓库没有最新版的npm rpm包。因此我改变了安装npm的方式。现在我执行以下命令:
yum install -y gcc-c++ make
curl -sL https://rpm.nodesource.com/setup_10.x | bash -
yum -y install nodejs
node -v
npm -v
npm install -g @angular/cli

根据https://tecadmin.net/install-latest-nodejs-and-npm-on-centos/,安装最新的Node.js和npm在CentOS上。

就像我之前所说的,如果你需要任何库的最新版本,无论是在yum还是npm中,你最终都会遇到这样的错误。这就是为什么软件要遵循semver的原因。坚持使用你的软件编写的固定版本。例如:@angular/cli@^8.0 - Mike Doe
人们可能想查看这份文档:https://docs.npmjs.com/about-semantic-versioning - Red Cricket
这样,当9.0版本发布时,您就不必想为什么您的应用程序突然崩溃了;) - Mike Doe

1
替换
npm install -g @angular/cli

在。
npm install -g @angular/cli@~8.0.0

当前 @angular/cli 版本为 8.0.4,存在依赖性问题。

建议使用 ~8.0.0 或更低版本(与 package.json 匹配最理想)。


我在CentOS上遇到了相同的错误。我有最新的node和npm(分别为v6.17.1和v3.10.10)。我尝试像你说的安装8.0.0版本,但是还是出现了相同的错误。错误代码:ELIFECYCLE。@angular/cli@8.0.6 postinstall脚本'node ./bin/postinstall/script.js'执行失败。 - Patrick

1

我在Windows上遇到了同样的错误:原来我使用的是一个非常老的版本(v.6.11.0)的Node,所以建议您尝试更新您的CentOS机器(或者您正在使用的任何环境)上的本地版本的Node。请注意,这也会更新您的npm版本(很可能)。


感谢@ivarcode。虽然我在Mac上遇到了这个问题。我想知道为什么当我像你一样使用brew install node(v.6.11.0)时,我的系统上安装了旧版本。因此,我的Angular也出现了同样的问题。然后我从https://nodejs.org/en/下载的软件包中安装了Node。 - Tul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接