npm : Postinstall not running in docker - node.js

I have a npm package (npm v 5.5.1 and node version is 9.2.0). If i run npm install on local machine then the postinstall defined in package.json is executed but if I run the same command RUN npm install in a Docker file i.e. when the command is run inside the container then the postinstall step is not executed. Any inputs what could be the issue here ?

Try running install with --unsafe-perm option. When running as root, npm won't run any scripts.
Alternatively, create a user in the Dockerfile and switch to that user:
FROM ...
RUN groupadd -r app && useradd -r -g app app
USER app

Another option to the selected answer (#yamenk's):
You could add this line to your dockerfile to configure your npm config inside the container (thus, allowing the execution of the postinstall script with the necessary permissions):
FROM ...
RUN npm config set unsafe-perm true

Related

Why doesn't my 'npm install --global yarn' work?

After running npm install --global yarn, I get an output that says:
> yarn#1.22.18 preinstall C:\Users\(me)\AppData\Roaming\npm\node_modules\yarn
> :; (node ./preinstall.js > /dev/null 2>&1 || true)
C:\Users\(me)\AppData\Roaming\npm\yarn -> C:\Users\(me)\AppData\Roaming\npm\node_modules\yarn\bin\yarn.js
C:\Users\(me)\AppData\Roaming\npm\yarnpkg -> C:\Users\(me)\AppData\Roaming\npm\node_modules\yarn\bin\yarn.js
+ yarn#1.22.18
updated 1 package in 0.389s
When I run "yarn serve" it tells me that 'yarn' is not recognized as a command. I've tried this in powershell and CMD and neither works.
It ended up being a path issue. I used npm list -g to reveal where yarn was installed. Then I searched for "Edit the environment variables" in Windows and added the containing folder to the PATH.
I also updated my node and npm to the latest versions before doing these steps.
Just put --> npm install -g yarn
The problem is that you're installing nvm as root, and when npm runs the lifecycle scripts it downgrades the permissions, making it impossible to even run Node scripts, since the node binary can only be accessed by root.

Local Npm module "jshint-stylish" not found. Is it installed?

Hi I am building docker image from node js project.
docker files contents are like below
fetch code from git repo.
install npm
install grunt cli
grunt build
please see below docker file
FROM ubuntu:latest
# Install Node.js and npm
RUN apt-get -y update
RUN apt-get -y install nodejs npm git wget curl
RUN git config --global http.sslverify false
WORKDIR /somerepo/
RUN git clone -somerepo
WORKDIR /somerepo/src/
RUN npm cache clean -f
RUN npm install -g n
RUN n stable
RUN npm install
RUN npm install -g grunt-cli
RUN grunt build
EXPOSE 3000
CMD ["node", "web.js"]
but its grunt build step is failing below error in logs
Step 14/15 : RUN grunt build
---> Running in d0f8863081f1
Local Npm module "jshint-stylish" not found. Is it installed?
Running "clean:dist" (clean) task
...
...
...
Running "githooks:all" (githooks) task
Binding jsbeautifier:pregit to pre-commit Git hook.
OK
Running "jshint:dev" (jshint) task
Warning: The "path" argument must be of type string. Received type object Use --force to continue.
Do any have any idea why grunt build is not ending successufully ?
I have already tried --save-dev option and also tried manually installing jshint with below command
#RUN npm install --save-dev jshint-stylish
Also i tried to print the files/directories having pattern "jshint" with below command in dockerfile
RUN find . -name "*jshint*"
and its printing below output in logs
Step 13/15 : RUN find . -name "*jshint*"
---> Running in db355c19f75a
./node_modules/.bin/jshint
./node_modules/ascii-table/.jshintrc
./node_modules/bl/.jshintrc
./node_modules/errno/.jshintrc
./node_modules/escodegen/.jshintrc
./node_modules/estraverse/.jshintrc
./node_modules/exit/.jshintrc
./node_modules/findup-sync/.jshintrc
./node_modules/gaze/.jshintrc
./node_modules/getobject/.jshintrc
./node_modules/globule/.jshintrc
./node_modules/grunt-contrib-clean/.jshintrc
./node_modules/grunt-contrib-compress/.jshintrc
./node_modules/grunt-contrib-concat/.jshintrc
./node_modules/grunt-contrib-cssmin/.jshintrc
./node_modules/grunt-contrib-jshint
./node_modules/grunt-contrib-jshint/tasks/jshint.js
./node_modules/grunt-contrib-jshint/tasks/lib/jshint.js
./node_modules/grunt-contrib-uglify/.jshintrc
./node_modules/grunt-contrib-watch/.jshintrc
./node_modules/grunt-express-server/.jshintrc
./node_modules/grunt-git-describe/.jshintrc
./node_modules/grunt-githooks/.jshintrc
./node_modules/grunt-img/node_modules/.bin/jshint
./node_modules/grunt-img/node_modules/grunt-contrib-jshint
./node_modules/grunt-img/node_modules/grunt-contrib-jshint/.jshintrc
./node_modules/grunt-img/node_modules/grunt-contrib-jshint/docs/jshint-examples.md
./node_modules/grunt-img/node_modules/grunt-contrib-jshint/docs/jshint-options.md
./node_modules/grunt-img/node_modules/grunt-contrib-jshint/docs/jshint-overview.md
./node_modules/grunt-img/node_modules/grunt-contrib-jshint/tasks/jshint.js
./node_modules/grunt-img/node_modules/grunt-contrib-jshint/tasks/lib/jshint.js
./node_modules/grunt-img/node_modules/grunt-contrib-jshint/test/jshint_test.js
./node_modules/grunt-img/node_modules/jshint
./node_modules/grunt-img/node_modules/jshint/packages/jshint
./node_modules/grunt-img/node_modules/jshint/packages/jshint/jshint.js
./node_modules/grunt-jsbeautifier/.jshintrc
./node_modules/grunt-json-replace/.jshintrc
./node_modules/grunt-legacy-log/.jshintrc
./node_modules/grunt-legacy-log-utils/.jshintrc
./node_modules/grunt-legacy-util/.jshintrc
./node_modules/grunt-lib-contrib/.jshintrc
./node_modules/grunt-manifest/.jshintrc
./node_modules/grunt-prettify/.jshintrc
./node_modules/grunt-sloc/.jshintrc
./node_modules/grunt-sloccount/.jshintrc
./node_modules/grunt-text-replace/.jshintrc
./node_modules/handlebars/.jshintrc
./node_modules/ibrik/node_modules/estraverse/.jshintrc
./node_modules/isstream/.jshintrc
./node_modules/istanbul/node_modules/estraverse/.jshintrc
./node_modules/jshint
./node_modules/jshint/bin/jshint
./node_modules/jshint/src/jshint.js
./node_modules/jshint-stylish
./node_modules/keep-alive-agent/.jshintrc
./node_modules/less/.idea/jsLinters/jshint.xml
./node_modules/less/.jshintrc
./node_modules/log4js/.jshintrc
./node_modules/nodemon/.jshintrc
./node_modules/phantomjs/.jshintrc
./node_modules/promise/.jshintrc
./node_modules/prr/.jshintrc
./node_modules/request-progress/.jshintrc
./node_modules/resolve-url/.jshintrc
./node_modules/restify/node_modules/qs/.jshintignore
./node_modules/restify/node_modules/qs/.jshintrc
./node_modules/save/.jshintignore
./node_modules/save/.jshintrc
./node_modules/shelljs/jshint.json
./node_modules/source-map-resolve/.jshintrc
./node_modules/source-map-url/.jshintrc
./node_modules/undefsafe/.jshintrc
./node_modules/urix/.jshintrc
./node_modules/xtend/.jshintrc
if anyone have any idea what is wrong please let me know
Thanks in advance !!!

How to add a custom CA-Certificate on an extended (node.js) docker image

I'm extending the node-red docker image which (currently) bases itself on the node:6docker image.
I would like to add a custom SSL-Certificate into the docker-image's certificate store. Up to now I did this as follow:
FROM nodered/node-red-docker
ADD DigiCertCA.crt /usr/local/share/ca-certificates/
RUN update-ca-certificates
ADD settings.js /data/settings.js
RUN npm install node-red-contrib-ttn
RUN npm install node-red-contrib-influxdb
RUN npm install node-red-admin
RUN npm install node-red-node-geohash
CMD ["npm", "start", "--", "--userDir", "/data"]
Building this image fails, because the RUN is executed as non-root user node.
Updating certificates in /etc/ssl/certs... ln: failed to create symbolic link '/etc/ssl/certs/DigiCertCA.pem': Permission denied
The command '/bin/sh -c update-ca-certificates' returned a non-zero code: 1
I'm aware that as non-root such an operation is not possible. But what's the valid concept to extend existing images with custom CA-Certificates?
Why not just switch user to root to run the command to add the cert then switch back?
FROM nodered/node-red-docker
ADD DigiCertCA.crt /usr/local/share/ca-certificates/
USER root
RUN update-ca-certificates
USER node-red
ADD settings.js /data/settings.js
RUN npm install node-red-contrib-ttn
RUN npm install node-red-contrib-influxdb
RUN npm install node-red-admin
RUN npm install node-red-node-geohash
CMD ["npm", "start", "--", "--userDir", "/data"]
This is a complete example with proxy and certificate.
Use npm config set cafile
Dockerfile:
FROM node:10.15.3-jessie
# HTTP Proxy
ARG http_proxy
ARG https_proxy
ENV http_proxy ${http_proxy}
ENV https_proxy ${https_proxy}
# Certicate
ENV CERT_HOME=/usr/local/share/ca-certificates
ENV CERT_FILE_PATH=${CERT_HOME}/my.crt
RUN mkdir -p ${CERT_HOME}
ADD my.crt ${CERT_FILE_PATH}
RUN apt-get update && apt-get install -y ca-certificates && rm -rf /var/lib/apt/lists/*
# npm settings
RUN npm config set cafile ${CERT_FILE_PATH}
RUN npm config set proxy ${http_proxy}
RUN npm config set https-proxy ${https_proxy}
# Check
RUN npm config get proxy
RUN npm config get https-proxy
RUN npm config get registry
And run:
docker build --build-arg http_proxy=$http_proxy --build-arg https_proxy=$https_proxy --tag mynode .

Jenkins is not installing some node dependencies

I'm running a nodejs application's build on jenkins. I run node as shell script step, because I have some limitations in terms of the plugins I can install in this jenkins instance.
This is what the npm install step looks like:
#!/bin/bash +x
source ~/.bashrc
cd $WORKSPACE/ && \
nvm use 7.8.0 && node --version && npm install
The problem I have is, when npm install finishes, it doesn't install everything. If I ssh into the box where jenkins is installed and run npm install inside that project's workspace, with the same user jenkins uses, it works ok. Any ideas?
EDIT
I just realized the dependencies that it's not installing are devDependencies
The problem was I had the node env set to production, and of course, it wouldn't get the devDependencies...

npm installing dev dependencies on production

I set NODE_ENV to production and tried to install dependencies using a Capfile which contains this:
run "cd #{latest_release} && npm config set production=true && npm install --production"
or this:
run "cd #{latest_release} && npm install --production"
but I always get also the dev dependencies, which is annoying because after a few releases all the inodes are taken and I cannot create any other files on the deploy machine.
I set the environment variable like this in the Capfile:
set :default_environment, {
'NODE_ENV' => 'production'
}
run "echo $NODE_ENV"
and it echoes the correct value.
If I execute
npm install --production
from within a shell, it works correctly. The user that makes the capistrano deploy and this shell user are the same, so I'm quite lost. Any hints?
The problem was:
sudo npm link
which I ran after the install command and which installs all dependencies. The fix is:
sudo npm link --production

Resources