0

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?

1
  • The Node source will never change once you've built the image; if you update the image you need to delete and recreate the container. So running nodemon doesn't add anything here and you could just run plain node. Commented Jul 19, 2020 at 10:28

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

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

1 Comment

npx command did not work inside the docker container.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.