问题描述

使用Gitlab的流水线部署前端项目时,执行install命令npm install -f --registry=https://registry.npm.taobao.org 突然报错

> esbuild@0.13.15 postinstall /root/builds/uzx3A_So/0/xxx/node_modules/esbuild
> node install.js

internal/modules/cjs/loader.js:905
  throw err;
  ^

Error: Cannot find module '/root/builds/uzx3A_So/0/xxx/node_modules/esbuild/install.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
    at Function.Module._load (internal/modules/cjs/loader.js:746:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

> vue-demi@0.13.11 postinstall /root/builds/uzx3A_So/0/xxx/node_modules/vue-demi
> node ./scripts/postinstall.js

internal/modules/cjs/loader.js:905
  throw err;
  ^

Error: Cannot find module '/root/builds/uzx3A_So/0/xxx/node_modules/vue-demi/scripts/postinstall.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
    at Function.Module._load (internal/modules/cjs/loader.js:746:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
npm notice created a lockfile as package-lock.json. You should commit this file.
added 312 packages from 279 contributors in 10.624s

28 packages are looking for funding
  run `npm fund` for details

问题原因

到node_mudules中查看,提示缺少的文件是存在的

到网上查找解决办法,然后确定是由于使用root用户执行npm命令引起。

npm 出于安全考虑不支持以 root 用户运行,如果用 root 用户身份运行了,npm 会自动转成一个叫 nobody 的用户来运行,而这个用户几乎没有任何权限。如果脚本里有需要写或执行的操作,就会由于权限不足,导致报错。

解决方案

1、执行命令是增加参数--unsafe-perm,如 npm install -f --unsafe-perm --registry=https://registry.npm.taobao.org

2、修改目录权限为777