19
votes

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.

8
Jenkins launches a new shell... - Malice
So you might want to source the update the PATh within the jenkins script itself.. - Malice
Yes, I updated the path within the jenkins shell itself, but still no luck. - oderfla
I used this tutorial (just the beggining) - Yuri Aps

8 Answers

13
votes

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

13
votes

I have been battling with this for some time now. Finally found the solution. From your jobs menu select Configure under Build Environment select Provide Node & npm bin/ folder to PATH You can leave the default setting and you are good to go.

enter image description here

As specified by Eric Wang in the comments, the NodeJS Plugin needs to be installed first for this option to come up: https://wiki.jenkins.io/display/JENKINS/NodeJS+Plugin

12
votes

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
votes

If you are using pipelines.

  1. Install the NodeJS Jenkins plugin
  2. Configure Node installation in the Global Tool Configurations Example
  3. Whenever you want to use npm in your pipeline use:

    nodejs('<name of your Node installation>'){
    
       //here your npm commands p.e.
    
       npm install
       npm run prod
    }
    
1
votes

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

1
votes

The only thing that worked for me in a pipeline scenario is adding the

tools {nodejs "Your Node JS Installation Name"}

After you install the Node JS plugin and add a new configuration in the Manage Jenkins->Global Tool Configuration menu.

0
votes

used nvm to install Node.js

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash

. ~/.nvm/nvm.sh

nvm install 4.4.5

node -v //4.4.5

Also,In jenkins, Manage Jenkins>>Global tool conf>> Nodejs installation

0
votes

At first, go to Manage Jenkins/Global Tool Configuration, configure nodejs installer like below.

enter image description here

Now go to Jenkinsfile and include the above tool.

tools {nodejs "NODEJS"}  //name should be similar to name used for installer in the global tool configuration.

This worked for me.