Azure DevOps构建管道中的`npm install`在node-gyp上失败

5

我们有一个在Azure DevOps中的CD/CI构建流水线,最近开始出现 nodejs/node-gyp 构建失败的问题。

  • 错误 gyp info 使用 node-gyp@3.8.0
  • 错误 gyp info 使用 node@16.13.0 | win32 | x64

我们的构建流水线没有做任何改变,所使用的资源池为:

pool:
  vmImage: 'windows-latest'

仍然映射到windows-2019https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/hosted?view=azure-devops&tabs=yaml

当我们在YAML文件中遇到以下任务时,管道会失败:

- task: Npm@1
  displayName: 'npm install'
  inputs:
    command: 'install'
    workingDir: 'FrontEnd'
    verbose: true

日志文件:

error gyp ERR! UNCAUGHT EXCEPTION
error gyp ERR! stack Error: spawn C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\15.0\Bin\MSBuild.exe ENOENT
error gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:282:19)
error gyp ERR! stack     at onErrorNT (node:internal/child_process:477:16)
error gyp ERR! stack     at processTicksAndRejections (node:internal/process/task_queues:83:21)
error gyp ERR! System Windows_NT 10.0.17763
error gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "D:\\a\\1\\s\\Presentation\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
error gyp ERR! cwd D:\a\1\s\FrontEnd\node_modules\node-sass
error gyp ERR! node -v v16.13.0
error gyp ERR! node-gyp -v v3.8.0
error gyp ERR! This is a bug in `node-gyp`.
error gyp ERR! Try to update node-gyp and file an Issue if it does not help:
error gyp ERR!     <https://github.com/nodejs/node-gyp/issues>
error Build failed with error code: 7
verbose exit 1

##[error]Error: Npm failed with return code: 1
4个回答

6
Azure自动更新了所使用的node版本,使得必须的node-gyp版本也随之推进,这导致我们所有的构建都失败了。它推送的版本如下:
  • npm@8.1.0
  • node@v16.13.0
task: NodeTool@0添加到代码中,以将Node版本设置为我们最新通过的版本,如下所示:
  • npm@6.14.15
  • node@14.18.1
最终的代码如下:
- task: NodeTool@0
  inputs:
    versionSpec: '14.x'
    
- task: Npm@1
  displayName: 'npm install'
  inputs:
    command: 'install'
    workingDir: 'FrontEnd'
    verbose: true

重新运行管道后,它再次正常工作并生成工件。

非常好用!非常感谢@Aaron! - Manish Gupta

4

对于经典编辑器用户:

  • 前往 Pipelines
  • 点击所需管道附近的“编辑”
  • 在 npm install 任务之前添加 Node.js 工具安装程序任务
  • 版本规范:14.x
  • 保存管道

1

目前你的回答不够清晰,请编辑并添加更多细节,以帮助其他人理解它如何回答问题。你可以在帮助中心找到有关如何撰写好答案的更多信息。 - Community

1
我们遇到的npm/node版本问题指向了node-gyp作为罪犯,如下所示。如果冲突的版本未在特定组件异常下显示,则在运行npm install时,npm/node版本将在用户代理下显示。
在npm install任务下,用户代理显示为:
"user-agent =“npm/6.14.15 node/v16.13.0 win32 x64”
当运行“windows-latest”镜像时。
使用上面答案中的node工具,上述内容更改为:
"user-agent =“npm/6.14.15 node/v14.18.1 win32 x64”
此问题类似于Azure DevOps Pipeline NPM install task fails with node-gyp build error

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