gigija3
gigija3

Reputation: 43

Error: Cannot find module '/usr/src/app/nodemon' while using docker-compose

I am trying to create a docker-compose.yml to be able to run my crud-admin panel using both my backend and frontend API together in the same network with docker. This is the first time i am using an "docker-compose" file to deploy with docker. Usually i deploy each API itself using a normal Dockerfile.

when running the command : docker-compose up --build i get the error:

Error: Cannot find module '/usr/src/app/nodemon'

I have:

before posting my Dockerfile and docker compose, here is my folder structure:

the-admin-panel
│
├──frontend(reactjs/nextjs)
│  ├─ .next
│  ├─ components
│  ├─ content
│  ├─ helper
│  ├─ pages
│  ├─ public
│  ├─ services
│  ├─ static
│  ├─ next.config
│  ├─ next-env.d
│  ├─ package.json
│  ├─ package-lock.json
│  ├─ tsconfig.json
│  ├─ Dockerfile
│  ├─ .dockerignore
│  
│
├──backend(nodejs)
│  ├─ api
│  ├─ startup
│  ├─ middleware
│  ├─ models
│  ├─ server.js
│  ├─ package.json
│  ├─ package-lock.json
│  ├─ .env
│  ├─ Dockerfile
│  ├─ .dockerignore
│  
├──docker-compose.yml

Currently, the frontend is not finished yet, so for now im only trying to deploy the backend, so im focusing on this part for now.

Here is the Dockerfile for my backend:

FROM node:13-alpine

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 5000
CMD [ "nodemon", "server.js"]

As you can see, im trying to run it with "nodemon".

And finally, here is my docker-compose.yml file(notice that i have commented all the lines within the frontend section as the frontend api is not finished yet)

version: '3'
services:

#set up the frontend
  #frontend:
  #  build:
  #    context: frontend
  #    dockerfile: Dockerfile
  #  image: frontend
  #    - $PWD:/usr/src/app
  #    - /usr/src/app/node_modules
  #  restart: always
  #  ports:
  #    - "3000:3000"
  #  networks:
  #    testnetwork

#set up the backend
  backend:
    build:
      context: backend
      dockerfile: Dockerfile
    image: nodejs-restapi
    #- $PWD:/usr/src/app
    #- /usr/src/app/node_modules

    restart: always
    ports:
      - "5000:5000"
    networks:
            testnetwork:


# Set up shared network
networks:
  testnetwork:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 10.0.0.0/8

in case if needed, here is my package.json file:

{
  "name": "..",
  "version": "1.0.0",
  "description": "..",
  "main": "server.js",
  "scripts": {
    "start": "nodemon server.js"
  },
  "author": "tim",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^5.0.1",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "fastest-validator": "^1.10.0",
    "helmet": "^4.4.1",
    "jsonwebtoken": "^8.5.1",
    "lodash": "^4.17.21",
    "mysql": "^2.18.1"
  },
  "devDependencies": {
    "nodemon": "^2.0.7"
  }
}

Could someone help me solve this nodemon issue?

Upvotes: 1

Views: 8653

Answers (3)

Charlie
Charlie

Reputation: 23798

I believe this is just a trial. So, without much production-grade adjustments, I would suggest you try changing your dockerfile.

FROM node:13-alpine

COPY . /usr/src/app
WORKDIR /usr/src/app

RUN npm install
RUN npm install -g nodemon

EXPOSE 5000
CMD [ "nodemon", "server.js"]

Upvotes: 1

gigija3
gigija3

Reputation: 43

Ok so thanks to Charlie, the solution was a small change in the Dockerfile where "RUN npm install -g nodemon" was added.

FROM node:13-alpine

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

RUN npm install -g nodemon  #this was needed in order to make it work

EXPOSE 5000

CMD [ "nodemon", "server.js"] 

Upvotes: 0

Ron B.
Ron B.

Reputation: 1540

I would simply suggest not using nodemon inside a docker image. nodemon is a tool for development, and I don't think there's much use for running it in production/any other environment you deploy to.

simply change the last line in your backend's Dockerfile to CMD [ "node", "server.js"]

Upvotes: 0

Related Questions