LarryDpk
发布于 2020-09-03 / 2516 阅读
0

在Jenkins pipeline中无法执行npm,报错Cannot run program npm

问题

搞前端项目ReactJSJenkins Pipeline,发现npm命令无法执行。报错大致如下:

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (n
pminstall) on project my-abc-web: Command execution failed. Cannot
 run program "npm" (in directory "XXX"): CreatePro
cess error=2, The system cannot find the file specified -> [Help 1]

其中是通过org.codehaus.mojo:exec-maven-plugin来执行npm的。

解决方案

配置pom时要路径:

<executable>${NPM_PATH}npm</executable>

通过maven的命令传参:mvn clean install -DNPM_PATH

如果无法这样直接传参(有些jenkins job无法直接传参),其中一个可行的方案是:在Jenkinsfile通过Groovy传环境变量${env.NPM_PATH},然后在jenkins slave机器配置环境变理即可。