Reputation: 1797
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.
Upvotes: 27
Views: 99599
Reputation: 1
Use This
n=$(which node);
n=${n%/bin/node};
chmod -R 755 $n/bin/*;
sudo cp -r $n/{bin,lib,share} /usr/local
Upvotes: 0
Reputation: 1307
pipeline {
agent any
stages {
stage("Install") {
steps {
sh "npm install"
}
}
stage("Build") {
steps {
sh "npm run build"
}
}
}
}
Upvotes: -3
Reputation: 494
At first, go to Manage Jenkins/Global Tool Configuration, configure nodejs installer like below.
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.
Upvotes: 6
Reputation: 31
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.
Upvotes: 3
Reputation: 475
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.
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
Upvotes: 28
Reputation: 61
If you are using pipelines.
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
}
Upvotes: 6
Reputation: 145
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
Upvotes: 0
Reputation: 11
After installing NodeJS restart you PC , npm will be visible to Jenkins
Upvotes: 1
Reputation: 4560
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
Upvotes: 21
Reputation: 10906
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.
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/
Upvotes: 13