Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have a permission to ask a question, You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

ErrorCorner

ErrorCorner Logo ErrorCorner Logo

ErrorCorner Navigation

  • Home
  • Contact Us
  • About Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Contact Us
  • About Us
Home/ Questions/Q 933
Answered
Kenil Vasani
Kenil Vasani

Kenil Vasani

  • 646 Questions
  • 567 Answers
  • 77 Best Answers
  • 26 Points
View Profile
  • 3
Kenil Vasani
Asked: December 22, 20202020-12-22T07:07:36+00:00 2020-12-22T07:07:36+00:00

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

  • 3

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 this Dockerfile:

FROM mhart/alpine-node
RUN mkdir -p /usr/src/app
RUN chmod -R 777 /usr/src/app
WORKDIR /usr/src/app
RUN npm install node-gyp -g
RUN npm install nodemon -g
ENV NODE_ENV development
EXPOSE 3000

And this portion of my docker-compose file:

backend:
    mem_limit: 100m
    build:
      context: .
      dockerfile: dockerfiles/node/Dockerfile
    command: npm start
    depends_on:
      - mongo
      - elasticsearch
    volumes:
      - ./backend/:/usr/src/app
    ports:
      - 3000:3000
    links:
      - "mongo:mongo"
      - "elasticsearch:elasticsearch"

When I do docker-compose up, I get this error:

backend_1        | npm info it worked if it ends with ok
backend_1        | npm info using npmRohit Jogani.10.10
backend_1        | npm info using [email protected]
backend_1        | npm info lifecycle service-designerRohit Patel.0.0~prestart: service-designerRohit Patel.0.0
backend_1        | npm info lifecycle service-designerRohit Patel.0.0~start: service-designerRohit Patel.0.0
backend_1        | 
backend_1        | > service-designerRohit Patel.0.0 start /usr/src/app
backend_1        | > nodemon index.js
backend_1        | 
backend_1        | [nodemon] 1.11.0
backend_1        | [nodemon] to restart at any time, enter `rs`
backend_1        | [nodemon] watching: *.*
backend_1        | [nodemon] starting `node index.js`
backend_1        | module.js:471
backend_1        |     throw err;
backend_1        |     ^
backend_1        | 
backend_1        | Error: Cannot find module 'dotenv'
backend_1        |     at Function.Module._resolveFilename (module.js:469:15)
backend_1        |     at Function.Module._load (module.js:417:25)
backend_1        |     at Module.require (module.js:497:17)
backend_1        |     at require (internal/module.js:20:19)
backend_1        |     at Object.<anonymous> (/usr/src/app/index.js:1:63)
backend_1        |     at Module._compile (module.js:570:32)
backend_1        |     at Object.Module._extensions..js (module.js:579:10)
backend_1        |     at Module.load (module.js:487:32)
backend_1        |     at tryModuleLoad (module.js:446:12)
backend_1        |     at Function.Module._load (module.js:438:3)
backend_1        | [nodemon] app crashed - waiting for file changes before starting...

If I do ls -al in the backend container, I get a full list of my backend app folder content, but it sounds like node_modules dependencies are not recognized.

dockerdocker-composenode.jsnpm
  • 1 1 Answer
  • 10 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook

    1 Answer

    • Voted
    1. Kenil Vasani

      Kenil Vasani

      • 646 Questions
      • 567 Answers
      • 77 Best Answers
      • 26 Points
      View Profile
      Best Answer
      Kenil Vasani
      2020-12-22T07:07:26+00:00Added an answer on December 22, 2020 at 7:07 am

      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.

      • 4
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    You must login to add an answer.

    Forgot Password?

    Sidebar

    Ask A Question
    • Popular
    • Kenil Vasani

      SyntaxError: invalid syntax to repo init in the AOSP code

      • 5 Answers
    • Kenil Vasani

      xlrd.biffh.XLRDError: Excel xlsx file; not supported

      • 3 Answers
    • Kenil Vasani

      Homebrew fails on MacOS Big Sur

      • 3 Answers
    • Kenil Vasani

      runtimeError: package fails to pass a sanity check for numpy ...

      • 3 Answers
    • Kenil Vasani

      Could not find tools.jar. Please check that /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home contains ...

      • 2 Answers

    Explore

    • Most Answered
    • Most Visited
    • Most Voted
    • Random

    © 2020-2021 ErrorCorner. All Rights Reserved
    by ErrorCorner.com