Docker - NPM install local packages - node.js

I am currently trying to install my NPM packages with Docker however, it's unable to do this for local packages? How do I fix this?
DockerFile:
FROM node:12
WORKDIR /var/api
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3010
CMD ["npm", "start"]
package.json
"dependencies": {
"#hapi/joi": "^16.1.7",
"#polka/send-type": "^0.5.2",
"polka": "^0.5.2",
"body-parser": "^1.19.0",
"axios": "^0.19.0",
"core": "file:../core",
"compression": "^1.7.4",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"ua-parser-js": "^0.7.21",
"moment": "^2.24.0",
"moment-duration-format": "^2.3.2",
"node-schedule": "^1.3.2"
},
Thanks.

COPY package*.json ./
RUN npm install
The problem is with those two lines, you are trying to install "core": "file:../core" but you never copied those dependencies to your image, so copy those dependencies along with the package.json and you will be good

Related

Docker build - version of npm is incompatible with lockfileVersion#1

I'm trying to make a build a Docker image of a react application.
Here is the first part of the package.json:
{
"name": "front",
"version": "0.1.0",
"private": true,
"dependencies": {
"#testing-library/jest-dom": "^5.14.1",
"#testing-library/react": "^11.2.7",
"#testing-library/user-event": "^12.8.3",
"eslint": "^7.29.0",
"html-react-parser": "^1.2.7",
"npm": "^6.14.4",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-input-mask": "^3.0.0-alpha.2",
"react-redux": "^7.2.4",
"react-router-dom": "^5.2.0",
"react-scripts": "^4.0.3",
"redux": "^4.1.0",
"semantic-ui-css": "^2.4.1",
"semantic-ui-react": "^2.0.3",
"web-vitals": "^1.1.2"
},
Here is the Dockerfile:
# pull official base image
FROM node:13.12.0-alpine
# set working directory
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
ARG REACT_APP_BASEURL='https://localhost:8081'
ENV REACT_APP_BASEURL=$REACT_APP_BASEURL
# install app dependencies
COPY package.json ./
COPY package-lock.json ./
RUN npm install
# add app
COPY . ./
#EXPOSE
EXPOSE 3000
# start app
CMD ["npm", "start"]
So, I build with the command line:
docker build -f Dockerfile -t api_front .
and I've this error message:
npm WARN read-shrinkwrap This version of npm is compatible with
lockfileVersion#1, but package-lock.json was generated for
lockfileVersion#2. I'll try to do my best with it!
If I check the version of npm with npm -v , I've the latest one (7.19.1)
If I go to the project folder and I run npm install , everything is up-to-date
If I start the application with npm start , everything is ok and the api is running...
What is the problem?
Edit: SOLUTION
I've changed:
FROM node:13.12.0-alpine
to
FROM node:latest
The version of npm (v7.19.1) used to generate the package-lock.json file is newer than the version of npm (v6.14.4) inside the docker image of node 13.12.0.
1: The lockfile version used by npm v5 and v6.
2: The lockfile version used by npm v7, which is backwards compatible to v1 lockfiles.
You can read more about lockfile versions here
You can either upgrade the docker image to use the lastest version or downgrade your npm to generate a compatible package-lock.json
EDIT #1:
You have to replace the first line of your docker file with:
FROM node:16.4.2

Segmentation fault (core dumped) in a running node process running in docker container

My docker image is running on AWS ECS. It runs for some time and then the container crashes with the following message :
Segmentation fault (core dumped)
npm ERR! code ELIFECYCLE
npm ERR! errno 139
npm ERR! fantasyservice#1.0.0 server: tsc -p . && node --max-old-space-size=1700 ./dist/server.js
npm ERR! Exit status 139
npm ERR!
npm ERR! Failed at the fantasyservice#1.0.0 server script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm WARN Local package.json exists, but node_modules missing, did you mean to install?
npm ERR! A complete log of this run can be found in:
npm ERR! /root/.npm/_logs/2020-07-08T07_51_00_148Z-debug.log
I found that this issue occurs when one of the libraries using C++ modules fails. My package.json has the following dependencies installed -
"dependencies": {
"#nestjs/graphql": "^6.5.3",
"#types/bull": "^3.10.6",
"#types/bull-arena": "^2.6.0",
"#types/check-types": "^7.3.1",
"#types/compression": "^1.0.1",
"#types/cors": "^2.8.6",
"#types/express": "^4.17.2",
"#types/graphql-type-json": "^0.3.2",
"#types/http-errors": "^1.6.2",
"#types/jsonwebtoken": "^8.3.5",
"#types/node": "^12.12.11",
"#types/node-fetch": "^2.5.3",
"#types/request": "^2.48.3",
"#types/request-promise": "^4.1.45",
"apollo-server-express": "^2.9.12",
"bcrypt": "^3.0.7",
"bull": "^3.12.1",
"bull-arena": "^2.6.4",
"check-types": "^11.0.0",
"compression": "^1.7.4",
"cookie-parser": "^1.4.4",
"core-util-is": "^1.0.2",
"cors": "^2.8.5",
"easygraphql-format-error": "0.0.3",
"express": "^4.17.1",
"express-graphql": "^0.9.0",
"express-winston": "^4.0.1",
"graphql": "^14.5.8",
"graphql-tools": "^4.0.6",
"graphql-type-json": "^0.3.1",
"http-errors": "^1.7.3",
"is-my-json-valid": "^2.20.0",
"jsonwebtoken": "^8.5.1",
"newrelic": "^6.3.0",
"object-mapper": "^6.0.1",
"pg": "^7.13.0",
"pug": "^2.0.4",
"redis": "^2.8.0",
"reflect-metadata": "^0.1.13",
"request": "^2.88.0",
"request-promise": "^4.2.5",
"segfault-handler": "^1.3.0",
"tsc-watch": "^4.0.0",
"tsconfig-paths": "^3.9.0",
"type-graphql": "^0.17.5",
"typeorm": "^0.2.20",
"typeorm-encrypted-column": "0.0.4",
"typeorm-naming-strategies": "^1.1.0",
"typescript": "^3.7.2",
"webpack-dev-server": "^3.9.0",
"winston": "^3.2.1",
"winston-daily-rotate-file": "^4.2.1",
"winston-loggly-bulk": "^3.0.1"
}
And my docker file is the following
FROM node:10
RUN apt update
RUN npm install typescript -g
COPY package*.json ./
RUN npm install
RUN npm rebuild bcrypt --build-from-source
ADD . /app
WORKDIR /app
CMD npm run server
I have tried increasing the ulimit, changed the node version in docker file and reduced the --max-old-space-size but to no effect. I also found out that npm rebuild bcrypt --build-from-source fixes an issue with bcrypt that causes this problem and have added that in my docker file link.

How to install npm pckage from private git repoistory using a token in github actions

I'm trying to install npm packages for my application within a Dockerfile. However, I get the following error when it comes to installing a package from a private git repository.
Step 9/10 : RUN npm ci
---> Running in 57960fe4df81
npm ERR! Error while executing:
npm ERR! /usr/bin/git ls-remote -h -t ***github.com/<redacted-private-org>/<redacted-private-repo>.git
npm ERR!
npm ERR! remote: Repository not found.
npm ERR! fatal: repository 'https://github.com/<redacted-private-org>/<redacted-private-repo>.git/' not found
npm ERR!
npm ERR! exited with error code: 128
Dockerfile
FROM node:12.18.0-alpine3.10
RUN apk update && apk upgrade && \
apk add --no-cache bash git openssh
RUN mkdir -p /home/dev
WORKDIR /home/dev
COPY . /home/dev
RUN npm ci
CMD ["node", "api/api.js"]
package.json
{
"name": "api",
"version": "0.1.0",
"author": "me",
"license": "",
"scripts": {
"prestart": "",
"start": "NODE_ENV=development nodemon ./api/server.js",
},
"dependencies": {
"bcrypt-nodejs": "^0.0.3",
"body-parser": "^1.18.2",
"org-common-utils": "git+https://<redacted-username>:<redacted-token>#github.com/<redacted-private-org>/<redacted-private-repo>.git",
"cors": "^2.8.4",
"dotenv": "^8.2.0",
"express": "^4.16.3",
"express-routes-mapper": "^1.1.0",
"helmet": "^3.12.0",
"igdb-api-node": "^3.1.7",
"jsonwebtoken": "^8.2.1",
"mysql": "^2.16.0",
"mysql2": "^1.6.4",
"node-cache": "^4.2.0",
"sequelize": "^5.21.3",
"sqlite3": "^4.0.0",
},
"devDependencies": {
"cross-env": "^5.1.4",
"eslint": "^4.19.1",
"eslint-config-airbnb-base": "^12.1.0",
"eslint-plugin-import": "^2.18.0",
"husky": "^0.14.3",
"jest": "^22.4.3",
"nodemon": "^1.17.3",
"shx": "^0.2.2",
"supertest": "^3.0.0"
}
}
To install from a private Github repository I'm using a username and token combination as you can see in my package.json.
The repository exists because if I try to navigate to the URL it loads when I'm logged in
https://github.com/redacted-private-org/redacted-private-repo
This issue is only occurring in github actions pipeline.
This issue was only occurring in github actions pipeline. It's solved by setting persist-credentials to false otherwise it uses github actions token which does not have the necessary permissions to pull/install the repository.
.
- name: Checkout
uses: actions/checkout#master
with:
persist-credentials: false
https://github.com/actions/checkout

"npm install" insall nothing to node_modules folder but a .staging folder centos 7

I have this package.json
{
"name": "jangkoo",
"version": "0.0.0",
"private": true,
"dependencies": {
"keystone": "^0.3.19",
"async": "^1.5.0",
"lodash": "^4.13.1",
"node-sass": "^3.3.2",
"node-sass-middleware": "^0.9.7",
"dotenv": "^2.0.0"
},
"devDependencies": {
"eslint": "^2.12.0",
"eslint-config-keystone": "^2.3.1",
"eslint-plugin-react": "^5.1.1",
"gulp": "^3.7.0",
"gulp-shell": "^0.5.0",
"gulp-watch": "^4.3.5",
"gulp-sass": "^2.0.4"
},
"scripts": {
"lint": "eslint .",
"start": "node keystone.js"
}
}
When i run npm install, it created a node_modules with a .staging folder inside. No other modules installed. What's the problem here? The OS is Centos 7 .
npm install temporarily adds all node_modules into a .staging folder during install. If something goes wrong during install, like you run out of memory, you could be stuck with this weird node_modules install containing the .staging folder.
https://github.com/npm/npm/issues/12540
for install npm for centos
yum update
yum install npm
I found it out. It failed to install keystone then it stopped. That's why nothing has been installed.
How about remove the node_modules folder and run npm install again

package.json add "npm install -g" to scripts preinstall

When writing package.json:
"scripts": {
"preinstall": "npm install -g grunt-cli"
},
"devDependencies": {
"async": "^0.9.0",
"grunt": "^0.4.5",
"grunt-contrib-connect": "^0.9.0",
"grunt-contrib-cssmin": "^0.10.0",
"grunt-contrib-jshint": "^0.10.0",
"grunt-contrib-uglify": "^0.6.0",
"grunt-contrib-watch": "^0.6.1",
"jit-grunt": "^0.9.0",
"marked": "^0.3.2",
"time-grunt": "^1.0.0",
"underscore": "^1.7.0"
}
And then running the command npm install
It works on some computers but on some I get this error:
http://pastebin.com/rYUwhV5k
Is it proper use of the package.json?
Is there any other way to do it?
How can I fix the problem that occurs on the other computers?
According to the npm documentation "NOTE: INSTALL SCRIPTS ARE AN ANTIPATTERN"
https://docs.npmjs.com/misc/scripts
In your case, probably best to just add in the readme.md that you need to have grunt installed to run the tests (and let the user decides how to install it)

Resources