Cannot find module for a node js app running in a docker compose environment Cannot find module for a node js app running in a docker compose environment docker docker

Cannot find module for a node js app running in a docker compose environment


You need to install the dependencies in the container, which is missing from your Dockerfile.

The common way is to create a Dockerfile that is already aware of your application, and make it copy your package.json file and perform an npm install.

This allows your container to find all your code dependencies when you later run your application.

See and example here:https://nodejs.org/en/docs/guides/nodejs-docker-webapp/

The sample Dockerfile:

FROM node:boron# Create app directoryRUN mkdir -p /usr/src/appWORKDIR /usr/src/app# Install app dependenciesCOPY package.json /usr/src/app/RUN npm install# Bundle app sourceCOPY . /usr/src/appEXPOSE 8080CMD [ "npm", "start" ]

You may need to adapt paths for the COPY command, of course.


If your Dockerfile and package.json files are correct and still have the issue:

  1. Make sure you've rebuilt your container images.

  2. Try

docker-compose down -v

before starting the containers again with docker-compose up.

This removes all volumes.


I also had the same issue when I run docker-compose up.

Issue resolved by running docker-compose up --build instead of docker-compose up.