docker-node: 'npm install' in a Dockerfile is not installing all my dependencies.

So it’s pretty strange, i don’t have any error, my missing module is listed in my package.json in my container, but the module is not in my node_modules folder.

Here’s my Dockerfile :

FROM node:8-alpine
RUN mkdir /app
WORKDIR /app
COPY package.json /app
RUN apk add --no-cache ffmpeg opus pixman cairo pango giflib ca-certificates \
    && apk add --no-cache --virtual .build-deps python g++ make gcc .build-deps curl git pixman-dev cairo-dev pangomm-dev libjpeg-turbo-dev giflib-dev \
    && npm install \
    && apk del .build-deps
COPY . /app
CMD ["npm", "start"]

Here’s my docker-compose.yml :

version: '3'
services:
  app:
    build: .
    volumes:
      - ./:/app
      - /app/node_modules
    restart: always
    env_file:
      - .env
    links:
      - mongo
    deploy:
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3

  mongo:
    image: mongo
    volumes:
      - ~/data:/data/db
    restart: always

  adminmongo:
    image: mrvautin/adminmongo
    ports:
      - 1234:1234
    environment:
      - HOST=0.0.0.0

And my dependencies package.json :

"dependencies": {
    "cleverbot-node": "^0.3.11",
    "discord.js": "github:discordjs/discord.js",
    "enmap": "^4.5.0",
    "googleapis": "^36.0.0",
    "jsonfile": "^5.0.0",
    "mongoose": "^5.3.15",
    "node-gyp": "^3.8.0",
    "node-opus": "^0.3.1",
    "nodemon": "^1.18.9",
    "request": "^2.88.0",
    "xml-js": "^1.6.8",
    "ytdl-core-discord": "^1.0.2"
  },

Just to precise the missing module is “jsonfile” but this is probably because it is the last i’ve installed. As i’ve said when i do a docker-compose build i don’t get any error but the ‘building’ of the container is abnormally long.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 22 (2 by maintainers)

Most upvoted comments

I think this might be caused by a combination of WORKDIR /app and your later steps.

WORKDIR sets the working directory of all subsequent steps, so package.json /app effectively copies to /app/app.

Try something like this (note that WORKDIR will create directories that do not already exist):

FROM node:8-alpine
WORKDIR /app
COPY package.json .
RUN apk add --no-cache ffmpeg opus pixman cairo pango giflib ca-certificates \
    && apk add --no-cache --virtual .build-deps python g++ make gcc .build-deps curl git pixman-dev cairo-dev pangomm-dev libjpeg-turbo-dev giflib-dev \
    && npm install \
    && apk del .build-deps
COPY . .
CMD ["npm", "start"]

Just sharing my two cents here in case anyone is facing a similar scenario:

I have a volume to share source code between host and container using docker-compose. This ended up deleting the node_modules folder on the container (because as a first time cloner, I didn’t install the dependencies on the host machine), so that caused my modules to not be found.

The solution I found was to include node_modules on my volumes

volumes:
  - /sillymistake/node_modules

@AnatoleLucet i got it working actually there was a tag which says devDependencies in package.json. And my env variable was set to prod because of which it was not installing certain dependencies.

I faced a similar problem when using docker-compose, however I do not understand this solution completely. Can you please explain why the COPY package.json . and subsequent COPY . /app is required, when the README for this repo states

Docker Compose example copies your current directory (including node_modules) to the container. It assumes that your application has a file named package.json defining start script.

Thanks for your help.

I have verified that the files like package.json are there in the correct location. I don’t copy the package-lock.json but I do copy the yarn.lock.

I even tried with and without copying package-lock.json but no help. Should i also try without copying yarn.lock file as well. @anatlole

FROM node:latest
#FROM ubuntu:18.04 
#RUN apt-get -y update
#RUN apt-get -y install nodejs
#RUN apt-get -y install npm
#RUN apt-get -y install vim
#RUN apt-get -y install curl

WORKDIR /app
COPY frontend ./frontend
COPY backend .
COPY backend/app .
COPY nodeInstall.sh .
COPY .npmrc /root
COPY ibmca.pem .
COPY redis.cert .
RUN npm install
RUN npm script build
CMD [ "node", "server.js" ]

@ AnatoleLucet

@rollrodrig if I remember correctly, it’s tricky to mount the entire root of the project using volumes like that. Can you try without the volume?

In my case, I was copying package-lock.json file after npm install in docker, which was causing problem. So I added package-lock.json and node-modules in .dockerignore file, and it work fine.

@AnatoleLucet I did try that but still no luck. Also, I am trying to exec into my docker pod and do npm i. Please see the snippet When i do npm i in my docker it installs only 183 packages whereas when I do it on my localhost it installs more than 1883 packages.

Screen Shot 2020-06-09 at 1 07 32 PM Screen Shot 2020-06-09 at 1 15 34 PM