GitLab CI/CD任务报错:bash: line 122: npm: command not found 求助
问题原因
你遇到的npm: command not found错误,是因为配置里的全局镜像ubuntu:18.04没有预装npm,而全局的before_script会在每个job运行前执行,这时在Ubuntu环境里跑npm install自然找不到命令。
解决方案
方案一:全局使用Node.js镜像(推荐)
直接把顶部的全局镜像换成带npm的Node.js镜像,整个流水线环境都自带npm,无需额外安装:
image: node:16.16.0 before_script: - npm install cache: paths: - node_modules unit-test: tags: - test script: - echo "Run unit testing" - npm run test - echo "Test successfully!"
注:unit-testjob原镜像和全局镜像一致,可删掉job里的image配置简化代码。
方案二:在全局before_script中安装npm(适合必须用Ubuntu镜像的场景)
如果流水线必须依赖Ubuntu 18.04环境,就在全局before_script里先安装nodejs和npm:
image: ubuntu:18.04 before_script: - apt-get update && apt-get install -y nodejs npm - npm install cache: paths: - node_modules unit-test: tags: - test image: node:16.16.0 script: - echo "Run unit testing" - npm run test - echo "Test successfully!"
方案三:将before_script移到job内部
如果只有unit-testjob需要执行npm install,可以把before_script放到job里,避免全局执行导致的环境不匹配:
image: ubuntu:18.04 cache: paths: - node_modules unit-test: tags: - test image: node:16.16.0 before_script: - npm install script: - echo "Run unit testing" - npm run test - echo "Test successfully!"
内容的提问来源于stack exchange,提问作者Alexander Wang
相关产品推荐
相关产品推荐

