You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自建GitLab Runner(shell执行器)无法找到npm命令的问题

解决GitLab Runner Shell执行器中npm: command not found的问题

以下是几种可行的解决思路:

1. 直接使用npm的绝对路径执行

先在服务器上通过which npm命令获取npm的绝对路径(例如/usr/local/bin/npm),然后修改CI配置文件中的脚本:

stages:
  - build

build:
  stage: build
  script:
    - /usr/local/bin/npm install
    - /usr/local/bin/npm run build
  artifacts:
    paths:
      - "public/dist/main.js"
  only:
    - master

2. 检查并修复环境变量PATH

在CI脚本中先打印当前环境的PATH,确认node/npm所在目录是否包含在内:

script:
  - echo $PATH
  - npm install
  - npm run build

如果输出的PATH里没有node/npm的路径(比如/usr/local/bin),需要将该路径添加到gitlab-runner用户的非交互式shell配置中:

  • 编辑/home/gitlab-runner/.bashrc(bash环境),添加一行:
    export PATH=$PATH:/usr/local/bin
    
  • 或者编辑/home/gitlab-runner/.profile,同样添加上述内容。

修改后无需重启GitLab Runner,下次CI任务会自动加载更新后的环境变量。

3. 验证gitlab-runner用户的非交互式环境

可以通过以下命令直接模拟gitlab-runner的执行环境,检查npm是否可用:

sudo -u gitlab-runner bash -c "npm -v"

如果这个命令也提示找不到npm,说明非交互式环境的PATH确实有问题,按照步骤2的方法修复即可。

内容的提问来源于stack exchange,提问作者sylbru

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 17:15:53