8

Prerequisites

Linux 18.04

create-react-app 2.0

docker 19.09.0

Dockerfile

# base image
FROM node:9.6.1

# set working directory
RUN mkdir /usr/src/app
WORKDIR /usr/src/app

# add `/usr/src/app/node_modules/.bin` to $PATH
ENV PATH /usr/src/app/node_modules/.bin:$PATH
ADD package.json /package.json

# install and cache app dependencies
COPY package.json /usr/src/app/package.json
RUN npm install --save --silent
RUN npm install react-scripts@latest -g --silent

# start app
CMD ["npm", "start"]

docker-compose.yml

version: '3.5'

services:

  provisioning-app:
    container_name: prv-app
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - '.:/usr/src/app'
      - '/usr/src/app/node_modules'
    ports:
      - '3000:3000'
    environment:
      - NODE_ENV=development

Problem

Everything has been working just fine, but after installing a new package docker stopped working as expected. Every time I run docker-compose up it fails with 'Module not found' error. While npm start works perfectly fine. I've tried quite a few workarounds found on the web:

  • restart PC
  • delete node_modules folder and run npm i again
  • different combinations of npm install / --save / including --save in Dockerfile

It just doesn't work and I can't handle it on my own. Any ideas why this might be happening?

Update

Weirdly, but this code works. I'm a rookie in Docker, so I have no clue what's the difference

docker run -it \
  -v ${PWD}:/usr/src/app \
  -v /usr/src/app/node_modules \
  -p 3000:3000 \
  --rm \
  prov-app

2 Answers 2

15

Everything has been working just fine, but after installing a new package docker stopped working as expected. Every time I run docker-compose up it fails with 'Module not found' error.

You need to rebuild your image to have that package installed. docker-compose build, and then docker-compose up

EDIT

Based on your update, I realize that the problem is your old node_module volume persisted between builds. Try docker-compose down -v before up.

Sign up to request clarification or add additional context in comments.

4 Comments

It is not a good idea to build and up container on every new package wich installed after first build.
I don't understand what you are suggesting
Neither did this one, doesn't woks as well
If you have db in the docker network you have to add data everytime. So in the long term this is not very viable
-2

Instead of:

volumes:
  - '.:/usr/src/app'
  - '/usr/src/app/node_modules'

Try:

volumes:
  - ./:/usr/src/app
  - /usr/src/app/node_modules

1 Comment

Nope, that made no difference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.