由于文件锁定,npm install 在 jenkins 上失败

npm install fails on jenkins due to file locking

我有一个在某些时候运行 npm install 的 powershell 构建脚本。 我在网上调查了这个问题,但找不到任何有用的信息。我看到有人建议在 Windows Defender 中为 jenkins 文件夹设置一个例外,因为它正在锁定文件。我做到了,还是不行。

我不认为脚本对我有任何锁定,我只是 运行 npm install。 奇怪的是,这并不总是发生,有时有效,有时无效。

26610 verbose stack Error: EPERM: operation not permitted, unlink 'C:\Jenkins\workspace\...\node_modules\.staging\highcharts-705cfa89\highcharts.src.js'
26612 verbose Windows_NT 10.0.18363
26613 verbose argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "install"
26614 verbose node v12.18.2
26615 verbose npm  v6.14.5
26616 error code EPERM
26617 error syscall unlink
26618 error path C:\Jenkins\workspace\...\node_modules\.staging\highcharts-705cfa89\highcharts.src.js
26619 error errno -4048
26620 error Error: EPERM: operation not permitted, unlink 'C:\Jenkins\workspace\...\node_modules\.staging\highcharts-705cfa89\highcharts.src.js'
26620 error  [OperationalError: EPERM: operation not permitted, unlink 'C:\Jenkins\workspace\...\node_modules\.staging\highcharts-705cfa89\highcharts.src.js'] {
26620 error   cause: [Error: EPERM: operation not permitted, unlink 'C:\Jenkins\workspace\...\node_modules\.staging\highcharts-705cfa89\highcharts.src.js'] {
26620 error     errno: -4048,
26620 error     code: 'EPERM',
26620 error     syscall: 'unlink',
26620 error     path: 'C:\Jenkins\workspace\...\node_modules\.staging\highcharts-705cfa89\highcharts.src.js'
26620 error   },
26620 error   errno: -4048,
26620 error   code: 'EPERM',
26620 error   syscall: 'unlink',
26620 error   path: 'C:\Jenkins\workspace\...\node_modules\.staging\highcharts-705cfa89\highcharts.src.js',
26620 error   parent: 'react'
26620 error }
26621 error The operation was rejected by your operating system.
26621 error It's possible that the file was already in use (by a text editor or antivirus),
26621 error or that you lack permissions to access it.
26621 error
26621 error If you believe this might be a permissions issue, please double-check the
26621 error permissions of the file and its containing directories, or try running
26621 error the command again as root/Administrator.
26622 verbose exit [ -4048, true ]

尝试 yarn install 而不是 npm install

对于 运行 关注此问题的任何人

package-lock.json 是问题所在。我们有一个我们使用的 npm 包的 proget 提要,但是 package-lock.json 将注册表设置为默认的 npm 注册表。这就是导致问题的原因。

解决方案是删除 package-lock.json 文件或在 运行 npm install 使用正确的注册表后提交它。