Jenkins Pipeline 流水线执行脚本时,可能会遇到 'npm: 未找到命令' 错误,即使 Jenkins 服务器已经安装了全局的 npm 包。

这可能是因为 Jenkins pipeline 流水线使用的是不同的环境变量,导致执行脚本时无法找到全局的 npm 包。

解决方法:

  1. 在 pipeline 中指定环境变量:

    您可以通过在 pipeline 中定义 environment 块来指定环境变量,将 npm 的路径添加到 PATH 环境变量中。例如:

    pipeline {
        agent any
        environment {
            PATH = "${tool 'NodeJS'}/bin:${env.PATH}"
        }
        stages {
            stage('Build') {
                steps {
                    sh 'npm install'
                    sh 'npm run build'
                }
            }
        }
    }
    

    其中 ${tool 'NodeJS'} 指的是 Jenkins 中已经安装的 NodeJS 工具的路径。

  2. 手动设置 npm 的路径:

    您也可以在脚本中手动指定 npm 的路径,例如:

    sh '/usr/local/bin/npm install'
    

    请将 /usr/local/bin/npm 替换为您的实际 npm 可执行文件路径。

注意:

  • 确保您的 Jenkins 服务器已经安装了 NodeJS 工具,并且在 Jenkins 中配置了 NodeJS 工具的路径。
  • 您可以使用 sh 'npm -v' 命令检查 npm 是否安装成功。
Jenkins Pipeline 流水线报错 'npm: 未找到命令' 解决方法

原文地址: https://www.cveoy.top/t/topic/nwn0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录