Azure DevOps Pipeline NPM 安装任务因 node-gyp 构建错误而失败

Azure DevOps Pipeline NPM install task fails with node-gyp build error

我正在尝试在 Azure DevOps 中构建 Azure Pipeline。问题是当管道 运行 时,在 npm 构建任务中发生错误。难道是package.json里面有一些过时的包?还是 npm 已经过时了?

请参阅管道开始 运行 的教程中的步骤 4: https://docs.microsoft.com/en-us/learn/modules/create-a-build-pipeline/6-create-the-pipeline

管道中构建任务的屏幕截图:

原始日志:

2021-12-03T00:13:53.4119601Z ##[section]Starting: Run npm install
2021-12-03T00:13:53.4130006Z ==============================================================================
2021-12-03T00:13:53.4130664Z Task         : npm
2021-12-03T00:13:53.4144256Z Description  : Install and publish npm packages, or run an npm command. Supports npmjs.com and authenticated registries like Azure Artifacts.
2021-12-03T00:13:53.4145177Z Version      : 1.187.0
2021-12-03T00:13:53.4145620Z Author       : Microsoft Corporation
2021-12-03T00:13:53.4146127Z Help         : https://docs.microsoft.com/azure/devops/pipelines/tasks/package/npm
2021-12-03T00:13:53.4146868Z ==============================================================================
2021-12-03T00:13:54.7198556Z [command]/usr/local/bin/npm --version
2021-12-03T00:13:55.4151672Z 8.1.0
2021-12-03T00:13:56.0808980Z [command]/usr/local/bin/npm config list
2021-12-03T00:13:56.3808693Z ; "env" config from environment
2021-12-03T00:13:56.3811792Z 
2021-12-03T00:13:56.3813271Z userconfig = "/home/vsts/work/1/npm/13.npmrc" 
2021-12-03T00:13:56.3814393Z 
2021-12-03T00:13:56.3815603Z ; node bin location = /usr/local/bin/node
2021-12-03T00:13:56.3816682Z ; cwd = /home/vsts/work/1/s
2021-12-03T00:13:56.3817732Z ; HOME = /home/vsts
2021-12-03T00:13:56.3819465Z ; Run `npm config ls -l` to show all defaults.
2021-12-03T00:13:56.3822946Z [command]/usr/local/bin/npm install
2021-12-03T00:16:21.9362133Z npm WARN old lockfile 
2021-12-03T00:16:21.9365129Z npm WARN old lockfile The package-lock.json file was created with an old version of npm,
2021-12-03T00:16:21.9369471Z npm WARN old lockfile so supplemental metadata must be fetched from the registry.
2021-12-03T00:16:21.9370832Z npm WARN old lockfile 
2021-12-03T00:16:21.9372351Z npm WARN old lockfile This is a one-time fix-up, please be patient...
2021-12-03T00:16:21.9373414Z npm WARN old lockfile 
2021-12-03T00:16:21.9375186Z npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.

'''
'''

2021-12-03T00:16:22.4541247Z 2732 error       |                                           ^
2021-12-03T00:16:22.4542220Z 2732 error /home/vsts/.node-gyp/16.13.0/include/node/node.h:855:3: note: in expansion of macro ‘NODE_MODULE_X’
2021-12-03T00:16:22.4543014Z 2732 error   855 |   NODE_MODULE_X(modname, regfunc, NULL, 0)  // NOLINT (readability/null_usage)
2021-12-03T00:16:22.4543629Z 2732 error       |   ^~~~~~~~~~~~~
2021-12-03T00:16:22.4544459Z 2732 error ../src/binding.cpp:358:1: note: in expansion of macro ‘NODE_MODULE’
2021-12-03T00:16:22.4545125Z 2732 error   358 | NODE_MODULE(binding, RegisterModule);
2021-12-03T00:16:22.4545958Z 2732 error       | ^~~~~~~~~~~
2021-12-03T00:16:22.4546611Z 2732 error make: *** [binding.target.mk:133: Release/obj.target/binding/src/binding.o] Error 1
2021-12-03T00:16:22.4547249Z 2732 error gyp ERR! build error
2021-12-03T00:16:22.4547826Z 2732 error gyp ERR! stack Error: `make` failed with exit code: 2
2021-12-03T00:16:22.4548944Z 2732 error gyp ERR! stack     at ChildProcess.onExit (/home/vsts/work/1/s/node_modules/node-gyp/lib/build.js:262:23)
2021-12-03T00:16:22.4550047Z 2732 error gyp ERR! stack     at ChildProcess.emit (node:events:390:28)
2021-12-03T00:16:22.4550822Z 2732 error gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)
2021-12-03T00:16:22.4551766Z 2732 error gyp ERR! System Linux 5.11.0-1021-azure
2021-12-03T00:16:22.4552997Z 2732 error gyp ERR! command "/usr/local/bin/node" "/home/vsts/work/1/s/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
2021-12-03T00:16:22.4554136Z 2732 error gyp ERR! cwd /home/vsts/work/1/s/node_modules/node-sass
2021-12-03T00:16:22.4554904Z 2732 error gyp ERR! node -v v16.13.0
2021-12-03T00:16:22.4555635Z 2732 error gyp ERR! node-gyp -v v3.8.0
2021-12-03T00:16:22.4556152Z 2732 error gyp ERR! not ok
2021-12-03T00:16:22.4556664Z 2732 error Build failed with error code: 1
2021-12-03T00:16:22.4557176Z 2733 verbose exit 1
2021-12-03T00:16:22.4557451Z 
2021-12-03T00:16:22.4593929Z ##[error]Error: Npm failed with return code: 1
2021-12-03T00:16:22.4611672Z ##[section]Finishing: Run npm install

为什么这个任务会出现错误?

Microsoft 在 12 月 3 日左右更新了他们的 VM。
在此更改中,默认节点版本已从 14.x 更改为 16.x。

我们在节点 16 上 运行ning 时发现了这个问题。

这不会是一个长期修复 - 因为您需要弄清楚为什么您的构建在 Node 16 上不起作用,但在短期内您可以添加一个命令来告诉构建代理使用节点的版本 14。

假设您使用的是现代 YAML 构建管道,请尝试在 YAML 管道中添加以下内容作为第一步:

- task: NodeTool@0
  inputs:
    versionSpec: '14.x'

这应该可以解决任何节点 16 问题。

如果您使用经典管道,可能有一种方法可以包括此步骤,因为它是 Microsoft 提供的,但我不确定,因为我不使用经典模式。

注:
如果您有 运行 npm 版本 8(节点 16 随附),它可能会将您的 package-lock.json 文件升级到较新的版本 2 架构。

此更新包-lock.json 不适用于版本 14,您需要将文件还原为版本 1(如果您在源代码管理中没有旧版本,则可以只需删除 package-lock 和 运行ning npm install).

您只需通过以下操作更新您的 packagelock 文件:

  1. 通过在节点 js 网站上安装 16.13.1 LTS 版本,将您的节点版本从 14 更新到 16:https://nodejs.org/

  2. 使用以下命令更新您的 npm 版本: npm install -g npm@latest

  3. 使用以下命令更新您的 packagelock.json: npm install

  4. 将您全新的 packagelock.json 文件推送到 Azure,然后再次启动您的 CI

=> 应该可以的^^

对于经典管道用户,在 npm 安装之前添加任务“Node.js 工具安装程序”。 Node.js 工具安装程序可以指定节点版本。

https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/tool/node-js?view=azure-devops