0

I run "docker compose up", and this is the error message:

recipeapp-server-1  | Error: Cannot find module 'express'
recipeapp-server-1  | Require stack:
recipeapp-server-1  | - /usr/src/app/index.js
recipeapp-server-1  |     at Module._resolveFilename (node:internal/modules/cjs/loader:1142:15)
recipeapp-server-1  |     at Module._load (node:internal/modules/cjs/loader:983:27)
recipeapp-server-1  |     at Module.require (node:internal/modules/cjs/loader:1230:19)
recipeapp-server-1  |     at require (node:internal/modules/helpers:179:18)
recipeapp-server-1  |     at Object.<anonymous> (/usr/src/app/index.js:1:17)
recipeapp-server-1  |     at Module._compile (node:internal/modules/cjs/loader:1368:14)
recipeapp-server-1  |     at Module._extensions..js (node:internal/modules/cjs/loader:1426:10)
recipeapp-server-1  |     at Module.load (node:internal/modules/cjs/loader:1205:32)
recipeapp-server-1  |     at Module._load (node:internal/modules/cjs/loader:1021:12)
recipeapp-server-1  |     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12) {
recipeapp-server-1  |   code: 'MODULE_NOT_FOUND',
recipeapp-server-1  |   requireStack: [ '/usr/src/app/index.js' ]
recipeapp-server-1  | }

Dockerfile:

FROM node

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

RUN num install express

COPY . .

EXPOSE 3000

CMD ["node", "index.js"]

With and without "RUN npm install express" give the same result.

docker-compose.yml:

version: '3.9'

services:
  server:
    volumes:
      - /usr/src/app/node_modules
    build: .
    ports:
      - '3001:3000'
  db:
    image: 'postgres'
    ports:
      - '4444:5432'
    environment:
      POSTGRES_USER: 'postgres'
      POSTGRES_PASSWORD: 'root'

With and without volumes give the same result.

package.json:

{
  "name": "recipeapp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.3",
    "pg": "^8.11.3"
  }
}

What I have tried:

  1. I have tried to add/remove "RUN npm install express" in Dockerfile and add/remove volume in docker-compose.yml.
  2. I have removed package-lock.json according to https://stackoverflow.com/a/75195936/14432555.
  3. I have add "node_process: container_name: container_node restart: unless-stopped" under server and changed "- /usr/src/app/node_modules" to "- ./node_process:/var/node_process - /var/node_process/node_modules" according to https://www.reddit.com/r/docker/comments/muqfds/comment/h8xvfng/?utm_source=share&utm_medium=web2x&context=3.
  4. I have changed "WORKDIR /usr/src/app" to "WORKDIR /usr/app" and "COPY . ." to "COPY ./src ." in Dockerfile, and changed volumes to "volumes: - ./src:/usr/app/" in docker-compose.yml according to https://forums.docker.com/t/dockerized-node-js-app-error-cannot-find-module/74280/2

The result of trying:

  1. Nothing works.
  2. Still doesn't work.
  3. I get the new error: "validating C:\Path\RecipeApp\docker-compose.yml: services.server Additional property node_process is not allowed"
  4. Still doesn't work.

What am I expecting: Work properly without any error.

6
  • 1
    Remove volumes from your docker compose file. You want the container to use the locally installed modules in the container, not ones from your system Commented Mar 11, 2024 at 19:58
  • 1
    This particular construct stores the node_modules directory in a Docker anonymous volume, so it will ignore changes to the package.json file and additional manual RUN npm install ... commands. Deleting it is right, especially if it's the only volume mount. Commented Mar 11, 2024 at 20:01
  • @AdamJenkins I removed volumes and still doesn't work Commented Mar 11, 2024 at 20:19
  • @Hin also move the COPY . . before npm install. You'll be overwriting node_modules with whatever you have in your local directory when running docker compose build, which is who knows what. Don't forget to rebuild the image. Commented Mar 11, 2024 at 20:21
  • @AdamJenkins I did both moving the COPY . . before npm install and rebuilding the image. It works now, thanks a lot, but I don't know which step solves the problem. Commented Mar 11, 2024 at 21:37

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.