Imagine that we need to update Node.js version on the Jenkins server. But it could affect other projects. For such cases, when we need multiple versions of Node.js on the same server the NVM (Node Version Manager) project exist.
Documentations - https://github.com/creationix/nvm. Nice tutorial: https://www.sitepoint.com/quick-tip-multiple-versions-node-nvm/.
NVM is the Bash command which will be installed locally, into user's home directory. Actually is not the script (file) - it is the Bash (Shell) function nvm:
jenkins@jenkins:~$ whereis nvm nvm: jenkins@jenkins:~$ which nvm jenkins@jenkins:~$ command -v nvm nvm
You can install multiple versions of Node.js (and appropriate npm) into the local directory and switch between them.
After the installation of NVM be sure that the following code has being added to the .profile:
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
jenkins@jenkins:~/.nvm$ nvm ls-remote v0.10.33 ... v6.11.3 (Latest LTS: Boron) jenkins@jenkins:~/.nvm$ nvm install 0.10.33 jenkins@jenkins:~/.nvm$ nvm install 6.11.3 jenkins@jenkins:~/.nvm$ nvm ls -> v0.10.33 v6.11.3 system default -> system node -> stable (-> v6.11.3) (default) stable -> 6.11 (-> v6.11.3) (default) iojs -> N/A (default) lts/* -> lts/boron (-> v6.11.3) lts/argon -> v4.8.4 (-> N/A) lts/boron -> v6.11.3 jenkins@jenkins:~/.nvm$ nvm alias default system jenkins@jenkins:~$ nvm use 6.11.3 Now using node v6.11.3 (npm v3.10.10)
NVM in Jenkins
Using NVM in Jenkins is a little bit tricky.
First of all - when running a Shell script from the job Jenkins use non-interactive session, so neither .profile no .bashrc files aren't executed. So nvm command is not set up automatically.
Also when set-up script (nvm.sh) consist of other command which returns non-zero exit status (even thought the main script itself will return 0) - the Jenkins' job will fail. That's because Jenkins use -e shell option. We have to disabled it temporary.
Also I have found that NVM directory path should be set. Beware of using ~ as home-directory alias - Jenkins override it to build working directory, so it is crucial to use $HOME variable.
Working example of Jenkins' Project config:
# Node Version Manager with Jenkins # https://github.com/creationix/nvm , https://www.sitepoint.com/quick-tip-multiple-versions-node-nvm/ set +ex # immediate script fail off, echo off export NVM_DIR="$HOME/.nvm" # set local path to NVM . ~/.nvm/nvm.sh # add NVM into the Shell session #nvm install v6.11.3 # first time only nvm use v6.11.3 # choose current version set -ex # immediate script fail on (default), echo on (default)