Error on docker build of React app: Cannot resolve module

I created an app using the create-react-app which works fine.
When I try to docker build with the following Dockerfile:

FROM node:7.7.4

ADD package.json /tmp/package.json
ADD public /tmp/public
ADD src /tmp/src
ADD .env /tmp/.env

RUN cd /tmp && \
    npm install && \
    npm run build  <-- failing on this step

I am getting the following error:
Module not found: Error: Cannot resolve module 'moment' in /tmp/src
and the build naturally fails.

  • Docker Rest Apis using node.js
  • Troubles using docker-machine to setup Swarm
  • Docker: Jenkins container can't access internet on QNAP device
  • ENV[“VARIABLE”].encoding return #<Encoding:ASCII-8BIT> on production when being assigned a unicode string
  • Docker Stunnel - Finding the IP address
  • Rails system test with capybara and headless selenium browser in Docker
  • I have tried adding the moment module in my package.json (npm install moment --save) but the error remained.

    What am I missing here?

  • docker/fig: port forwarding to guest machine does not work on MacOS
  • Cannot remove all containers in a row
  • What is the difference between CMD and ENTRYPOINT in a Dockerfile?
  • Can I extend docker-compose files?
  • How to deploy Mule Application on Kubernetes?
  • Docker and Java IDE integration
  • One Solution collect form web for “Error on docker build of React app: Cannot resolve module”

    After some fiddling around in the containers, I discovered that the node_module folder does not pass with all it’s dependencies in the docker container. So there are two ways to fix this:

    1. The economic one:
      add your dependencies in the package.json file and on the npm install time everything gets assorted. (That fixed my problem!)
    2. If you have a local build which you don’t share over Git and you are not using a .dockerignore file as I did, you can simply add everything from your development folder to the container as follows:

      ADD package.json /tmp/package.json
      ADD . .

      replacing the other ADD lines I had in there.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.