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

前端 未结 3 905
无人共我
无人共我 2021-02-05 00:38

I am sorry for my very newbie question, but I am having a terrible day figuring out this error, I have an Express app and I am trying to run it in docker compose. I\'ve used thi

相关标签:
3条回答
  • 2021-02-05 00:51

    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.

    0 讨论(0)
  • 2021-02-05 00:57

    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.

    0 讨论(0)
  • 2021-02-05 01:01

    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.

    0 讨论(0)
提交回复
热议问题