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

After adding npm install to the Dockerfile, add node_modules to volumes

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

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 directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

EXPOSE 8080
CMD [ "npm", "start" ]

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


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.


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.