Enzio
Enzio

Reputation: 889

Running a nodejs app on a docker container gives " Error: Cannot find module '/usr/src/app/nodemon' "

Here is my Dockerfile which is at the root of the nodejs application.

# Build from  LTS version of node (version 12)
FROM node:12

# Create app directory
RUN mkdir -p /usr/src/app

# Define app diretory inside image
WORKDIR /usr/src/app

# package.json AND package-lock.json are copied where available 
COPY package*.json /usr/src/app/

# install modules
RUN npm install

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

# Bind app to port 3000
EXPOSE 3000

# Command to run app
CMD [ "nodemon", "./bin/www" ]

Here is my docker-compose.yml file

version: '2'
services:
  mongo:
    container_name: mongo
    image: 'mongo:3.4.1'
    ports:
      - "27017:27017" 
  backend-app:
    container_name: school-backend
    restart: always
    build: ./server
    ports:
      - "3000:3000"
  frontend-app:
    container_name: angular-frontend
    restart: always
    build: ./angular-app
    ports:
      - "4200:4200"

I execute the command docker-compose up

Then I get this error

school-backend  | Error: Cannot find module '/usr/src/app/nodemon'
school-backend  |     at Function.Module._resolveFilename (internal/modules/cjs/loader.js:966:15)
school-backend  |     at Function.Module._load (internal/modules/cjs/loader.js:842:27)
school-backend  |     at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
school-backend  |     at internal/main/run_main_module.js:17:47 {
school-backend  |   code: 'MODULE_NOT_FOUND',
school-backend  |   requireStack: []
school-backend  | }

In the Dockerfile, I copy the package.json to the working directory /usr/src/app\.

Then I do npm install which would install nodemon since it is declared in the package.json

But, why is the module given as absent?

Upvotes: 0

Views: 1038

Answers (2)

Daniel
Daniel

Reputation: 2531

You can use npx like this CMD [ "npx", "nodemon", "./bin/www" ].

npx will run programs from the node_modules/.bin directory.

Upvotes: 1

tpschmidt
tpschmidt

Reputation: 2727

It's not globally installed then.

In this case, you have to call the nodemon bin inside the node_modules: ./node_modules/nodemon/bin/nodemon.js.

Upvotes: 1

Related Questions