GitLab Action中以root权限执行npm install仍报EACCES错误的解决方案求助
GitLab Action中以root权限执行npm install仍报EACCES错误的解决方案求助
我翻遍了GitHub的issue和相关技术帖子,都没找到解决这个问题的办法,试过常见的EACCES权限问题修复方案也没用。
我现在在GitLab CI里构建前端项目,用的是node:latest镜像,执行whoami显示当前是root用户,但还是遇到了EACCES权限错误。
我的CI构建阶段配置是这样的:
build-frontend: stage: build image: node:latest variables: NPM_CONFIG_PREFIX: ~/.npm-global # Use a custom npm directory script: - mkdir -p ~/.npm-global - chmod -R 777 ~ - chmod -R 777 . - cd net-hr-report-frontend - npm ci || (chmod +x node_modules/esbuild/bin/esbuild && npm ci) - npm run build - echo "Build complete, check if backend has newest frontend changes" - git diff --exit-code
CI日志里的错误信息如下:
Running with gitlab-runner 17.4.0 (b92ee590) on Gitlab Runner 2 JitPAe4N, system ID: s_5cb67d47bc0f Preparing the "docker" executor 00:03 Using Docker executor with image node:latest ... Pulling docker image node:latest ... Using docker image sha256:1e7add1c04c7db775558644afde13b9e362a51fa6452cc617972147a70a7fba3 for node:latest with digest node@sha256:c65ab339c494443a7d2ed36140674bc8815f7100b0d4cebfe0144ce35267a693 ... Preparing environment 00:01 Running on runner-jitpae4n-project-1254-concurrent-0 via gitlab-runner-2.dmz.xxxx.ca... Getting source from Git repository 00:02 Fetching changes with git depth set to 20... Reinitialized existing Git repository in /builds/*****/******/.git/ Checking out 515b5f82 as detached HEAD (ref is action)... Skipping Git submodules setup Restoring cache 00:01 Checking cache for default-non_protected... No URL provided, cache will not be downloaded from shared cache server. Instead a local version of cache will be extracted. Successfully extracted cache Executing "step_script" stage of the job script 00:10 Using docker image sha256:1e7add1c04c7db775558644afde13b9e362a51fa6452cc617972147a70a7fba3 for node:latest with digest node@sha256:c65ab339c494443a7d2ed36140674bc8815f7100b0d4cebfe0144ce35267a693 ... $ mkdir -p ~/.npm-global $ chmod -R 777 ~ $ chmod -R 777 . $ cd frontend $ npm ci || (chmod +x node_modules/esbuild/bin/esbuild && npm ci) (node:18) ExperimentalWarning: CommonJS module /usr/local/lib/node_modules/npm/node_modules/debug/src/node.js is loading ES Module /usr/local/lib/node_modules/npm/node_modules/supports-color/index.js using require(). Support for loading ES Module in require() is an experimental feature and might change at any time (Use `node --trace-warnings ...` to show where the warning was created) npm error code 1 npm error path /builds/*****/******/frontend/node_modules/esbuild npm error command failed npm error command sh -c node install.js npm error node:internal/child_process:1120 npm error result.error = new ErrnoException(result.error, 'spawnSync ' + options.file); npm error ^ npm error npm error <ref *1> Error: spawnSync /builds/*****/******/frontend/node_modules/esbuild/bin/esbuild EACCES npm error at Object.spawnSync (node:internal/child_process:1120:20) npm error at spawnSync (node:child_process:868:24) npm error at Object.execFileSync (node:child_process:911:15) npm error at validateBinaryVersion (/builds/*****/******/frontend/node_modules/esbuild/install.js:99:28) npm error at /builds/*****/******/frontend/node_modules/esbuild/install.js:283:5 { npm error errno: -13, npm error code: 'EACCES', npm error syscall: 'spawnSync /builds/*****/******/frontend/node_modules/esbuild/bin/esbuild', npm error path: '/builds/*****/******/frontend/node_modules/esbuild/bin/esbuild', npm error spawnargs: [ '--version' ], npm error error: [Circular *1], npm error status: null, npm error signal: null, npm error output: null, npm error pid: 0, npm error stdout: undefined, npm error stderr: undefined npm error } npm error npm error Node.js v23.2.0 npm error A complete log of this run can be found in: /root/.npm/_logs/2024-11-19T19_52_13_686Z-debug-0.log chmod: cannot access 'node_modules/esbuild/bin/esbuild': No such file or directory Cleaning up project directory and file based variables
我已经尝试了很多方法,但都没效果:
- 试过多个Node版本:latest、20、18等
- 加了
--allow-root参数执行依赖安装,还是报同样的错 - 尝试用
su root -c执行npm install,没用 - 试过
chown修改权限,也没解决 - 试过全局安装依赖,但后续执行
npm run build时提示vite找不到,所以必须本地安装 - 本地Windows机器上执行npm install完全正常,但GitLab CI就是不行
- 升级了所有依赖,警告消失了但错误依旧
这是我的package.json内容:
{ "name": "frontend", "private": true, "version": "0.0.0", "type": "module", "scripts": { "dev": "vite --mode development", "build": "vite build --mode production --emptyOutDir", "lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0", "preview": "vite preview" }, "dependencies": { "@tanstack/react-query": "^5.40.1", "@vitejs/plugin-basic-ssl": "^1.1.0", "antd": "^5.19.0", "net-hr-contactinfo-frontend": "file:", "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "^6.23.1" }, "devDependencies": { "@types/react": "^18.2.66", "@types/react-dom": "^18.2.22", "@vitejs/plugin-react": "^4.2.1", "eslint": "^8.57.0", "eslint-plugin-react": "^7.34.1", "eslint-plugin-react-hooks": "^4.6.0", "eslint-plugin-react-refresh": "^0.4.6", "sass": "^1.77.4", "vite": "^5.2.0" } }
实在没辙了,之前做过很多Node项目的CI/CD都没遇到过这种情况,有没有大佬能指点一下?
备注:内容来源于stack exchange,提问作者Jiajun Huang
相关产品推荐
相关产品推荐

