Jenkins unable to find npm

2019-04-22 04:26发布

问题:

On this linux server, I have a user named "myuser". For this user, when echoing the path, I get this:

/home/myuser/bin:/home/myuser/.local/bin:/home/myuser/.nvm/versions/node/v6.11.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Having a node application, when deploying manually I run:

npm i

And it works.

Now, I installed Jenkins. Jenkins project I'm trying to install is located at:

/var/lib/jenkins/workspace/test

The build is executing a shell script. In that window I entered:

#!/bin/bash
npm i

When building with Jenkins, I get this:

[test] $ /bin/bash /tmp/jenkins756533162549346948.sh
/tmp/jenkins756533162549346948.sh: line 3: npm: command not found
Build step 'Execute shell' marked build as failure
Finished: FAILURE

If I only write:

echo $PATH

in the Jenkins shell, I get this:

[test] $ /bin/sh -xe /tmp/jenkins5067097808572366507.sh
+ echo /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/snap/bin
[test] $ /var/lib/jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/6.11.1/bin/node /tmp/jenkins8733250738704177758.js
Finished: SUCCESS

As you can see, I installed nodejs plugin. Anyway, when using Jenkins shell, the npm and even node are not found. How can I make Jenkins to know where npm/node is? I have tried to first write this in the shell:

$PATH=/home/myuser/.nvm/versions/node/v6.11.1/bin

But still no luck.

回答1:

Just install the nodeJS plugin for jenkins, you can find it here.

After installing the plugin, restart jenkins, and go to the global configs to specify the version.

The full details of configurations can be found in the plugin documentation linked above.


Update for jenkins 2.x

To get to the plugin page in jenkins 2.x:

simply go to Manage Jenkins > Manage Plugins view, available to administrators of a Jenkins environment. - https://jenkins.io/doc/book/managing/plugins/

However, I do recommend using pipelines instead of a plugin for the CI process:

Pipelines are instructions to describe portions of your software delivery pipeline.

Add this pipeline config to your node.js project on jenkins to have it running.

pipeline {
    agent {
        docker {
            image 'node:6-alpine'
            args '-p 3000:3000'
        }
    }
    environment {
        CI = 'true' 
    }
    stages {
        stage('Build') {
            steps {
                sh 'npm install'
            }
        }
        stage('Test') { 
            steps {
                sh './jenkins/scripts/test.sh' 
            }
        }
    }
}

As you can see this runs two stages, building and testing for the application. npm is installed through the docker image node:6-alpine.

Jenkins docs provide a full tutorial to build a nodejs app through CI: https://jenkins.io/doc/tutorials/build-a-node-js-and-react-app-with-npm/



回答2:

The answers in this thread didn't help me, what helped was adding the node.js tool to my Jenkinsfile:

pipeline {
  agent any

  tools {nodejs "nodejs"}

  stages {
    stage('Example') {
      steps {
        sh 'npm config ls'
      }
    }
  }
}

Where the string "nodejs" is the name you give the node.js tool in the global tool configuration



回答3:

After installing NodeJS restart you PC , npm will be visible to Jenkins