Add nodejs and npm to Jenkins docker image - node.js

I'm trying to do a quick experiment or two with Jenkins. For this I intended to use the jenkins image and add nodejs and npm to it. Here's what I'm trying to do:
FROM jenkins
USER root
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash - \
&& apt-get update \
&& apt-get install -y \
nodejs
RUN npm install -g npm
RUN npm --version
USER jenkins
The npm --version call will fail though, with this error:
Step 5/6 : RUN npm --version
---> Running in 48a250a4fdb8
module.js:471
throw err;
^
Error: Cannot find module 'process-nextick-args'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/usr/lib/node_modules/npm/node_modules/readable-stream/lib/_stream_readable.js:26:23)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
The command '/bin/sh -c npm --version' returned a non-zero code: 1
What is the proper way (if any) to fix this?

Try the following, it uses docker multi-stage build to copy dependencies from one image to another. This will allow you to avoid all the special things that need to be done to install node in docker. There is an official docker image that is built by the docker team, so you use that as follows:
FROM node
FROM jenkins
USER root
COPY --from=0 /usr/local /usr/local
RUN npm --version
USER jenkins
npm --version will output 5.3.0

Jenkins image by docker uses minimal installation for running Jenkins
FROM node:15.4 as nodejs
ENV JENKINS_HOME /var/jenkins_home
USER root
RUN rm -rf /var/lib/apt/lists/*
RUN rm -rf /etc/apt/sources.list.d/*
RUN apt-get update
#INSTALL NODE
COPY --from=nodejs /usr/local /usr/local
RUN node -v
RUN npm -v

Related

Not able to run nodemon (SyntaxError: Unexpected token) on ubuntu

command I am running : nodemon server.js,
error I am getting :
nodemon server.js
/usr/local/lib/node_modules/nodemon/node_modules/update-notifier/index.js:58
} catch {
^
SyntaxError: Unexpected token {
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/usr/local/lib/node_modules/nodemon/bin/nodemon.js:15:3)
I tried various ways of installing nodemon on Ubuntu but still getting exact same error every time I run.
One reason could be your node.js installation is outdated.
You should upgrade to the recent version of node.js
You can check the version with:
node -v
If the version is recent, then you should try installing nodemon globally with this command
npm install -g nodemon # or using yarn: yarn global add nodemon
If you are stuck in an old version of node.js then do this:
sudo apt-get remove nodejs
sudo apt-get remove npm
Then go to /etc/apt/sources.list.d and remove any node list if you have. Then do a
sudo apt-get update
Check for any .npm or .node folder in your home folder and delete those.
If you type
which node
you can see the location of the node. Try which nodejs and which npm too.
Then do a fresh installation like this
cd ~
curl -sL https://deb.nodesource.com/setup_16.x -o nodesource_setup.sh
nano nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt install nodejs

Truffle Fails on Start After Install

I installed Truffle just fine, but I can't call it without getting the following error:
/usr/local/lib/node_modules/truffle/build/cli.bundled.js:98908
function VM (opts = {}) {
^
SyntaxError: Unexpected token =
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:374:25)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:442:10)
at startup (node.js:136:18)
at node.js:966:3
System Spec:
uname -a
Linux 7ed9621f5250 4.1.12-103.7.1.el7uek.x86_64
NodeJS v4.2.6
npm v3.5.2
Install commands used:
apt-get update
apt-get -y install curl nodejs npm
ln -s /usr/bin/nodejs /usr/bin/node
npm install -g truffle
I can't find any docs or posts on this in order to troubleshoot. Please help. Thanks.
I was also stuck in this. I followed this which work for me, hope it also works for you.
Remove node and npm completely. Install fresh node and npm using :
wget https://nodejs.org/dist/v8.8.0/node-v8.8.0-linux-x64.tar.gz
sudo tar -xf node-v8.8.0-linux-x64.tar.gz --directory /usr/local --strip-components 1
node --version
npm --version
sudo npm install -g truffle
mkdir truffle-proj
cd truffle-proj
truffle init

NPM breaks after it has been upgraded in docker container

I need to create a docker container with node v6.10.3, but with the latest npm (currently v5.4.1) to use new npm features for local packages.
Such installation runs without any problem on my Mac, but when I try to create a docker image with such an installation, after updating npm, the npm tool gets broken and throws a bunch of errors about missing packages.
Here is the example of the Dockerfile, with which I can reproduce this issue (note that my real Dockerfile is more complex):
FROM ubuntu:xenial
RUN apt-get update
RUN apt-get install -y curl
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get install -y nodejs
RUN npm i -g npm
RUN npm i -g lerna
When the build process gets to the line RUN npm i -g lerna it throws a bunch of errors like:
Error: Cannot find module 'process-nextick-args'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/usr/lib/node_modules/npm/node_modules/readable-stream/lib/_stream_readable.js:26:23)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
Any other npm script would result in the same errors. Reinstalling all the packages on which npm depends, does not seem as a solution for me.
I have also tried to install node inside the container using nvm, but I got the same errors.
My docker version:
Docker version 17.06.2-ce, build cec0b72
What's wrong with this Dockerfile and what am I missing?
I have found a workaround for this issue, using yarn.
It looks weird, but it works:
FROM ubuntu:xenial
RUN apt-get update
RUN apt-get install -y curl
RUN curl -sL https://deb.nodesource.com/setup_6.x | bash -
RUN apt-get install -y nodejs
RUN npm i -g yarn
RUN npm uninstall npm -g
RUN yarn global add npm
RUN npm i -g lerna
Still, it would be great if somebody can explain why the original solution did not work, and/or help find a better way to fix it.

Error with nodejs and express

I am trying to use express in an ubuntu 13.10 distribution. I am using node version 0.10.29 and npm 1.4.14 When I use express this is my output:
module.js:340
throw err;
^
Error: Cannot find module 'mkdirp'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/usr/bin/express:10:14)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
Additionally, I have to use sudo npm as npm doesn't work (i'm not sure if that's part of the issue - but I suspect path issues, not really sure). I have googled but haven't been able to fix the issues.
Edit: I think I messed up my path. I am unable to reinstall npm. Here is output from $PATH
bash: /home/qtrain/npm/bin:/usr/bin/yo:/home/qtrain/.node/bin:/home/qtrain/Enthought/Canopy_64bit/User/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/qtrain/.rvm/bin:/home/qtrain/.rvm/bin:/home/qtrain/.rvm/bin:
The mkdirp error seems to originate from using the chris-lea repo for node.js as Michael answered.
To fix: eliminate the PPA and node itself, then reinstall as below.
1.) sudo apt-get install ppa-purge - if you don't already have it
2.) sudo ppa-purge ppa:chris-lea/node.js
3.) sudo apt-get remove nodejs
4.) sudo apt-get remove npm
5.) sudo apt-get autoremove
6.) sudo apt-get install nodejs
7.) sudo apt-get install npm
8.) sudo apt-get install node-express
I ran into a similar problem while using npm and express. My solution was to uninstall node from my entire system using sudo apt-get remove nodejs
Then I reinstalled node from source following directions here http://chimera.labs.oreilly.com/books/1234000001808/ch01.html#chap5_id35941617
Then I had to install npm separately using sudo apt-get install npm but it still didn't work.
It was then that I realized that ppa repos I had installed from chris lea were not configured for 13.10
https://launchpad.net/~chris-lea/+archive/ubuntu/node.js-legacy
Removed them and reinstalled npm using sudo apt-get install npm and installed express with sudo apt-get install node-express
I was then able to use npm and express
Hope this helps..

Node forever doesn't work on Ubuntu 12.04.1

When I fun forever on Ubuntu 12.04.1, I got the error below after upgrading node.js from v0.6.12 to v0.8.15. Before upgrading it works well. Does anyone encounter this kind of issue before?
$ forever
module.js:340
throw err;
^
Error: Cannot find module './daemon.v0.8.15'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/usr/local/lib/node_modules/forever/node_modules/daemon/lib/daemon.js:12:11)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:362:17)
My environment is
node v0.8.15
npm 1.1.66
I installed node and npm with the command line blow
$ sudo apt-get install python-software-properties -y
$ sudo add-apt-repository ppa:chris-lea/node.js
$ sudo apt-get update
$ sudo apt-get install nodejs npm -y
I installed forever with the command line
$ sudo npm install forever -g
'sudo npm update -g forever' doesn't work for me. I deleted all the installed modules and install it again. The forever works. The steps are as below.
Delete the all the existing forever modules.
$ sudo rm -R /usr/lib/node_modules/forever
$ sudo rm -R /usr/local/lib/node_modules/forever
Install the forever again.
$ sudo npm install forever -g
You will get he solution with below commands but some issue, so please try the PM2 with so many advantages and check points of debuting
$ sudo rm -R /usr/lib/node_modules/forever
$ sudo rm -R /usr/local/lib/node_modules/forever
Please use this for debuting the node.js exception and monitoring the process of each and every with clustering and master and slave logic
http://devo.ps/blog/2013/06/26/goodbye-node-forever-hello-pm2.html
$ sudo npm install -g pm2

Resources