The http-errors package exists, but the Node.js container does not see it

Hi,
The YAML file is as follows:

services:
    nginx:
      container_name: Nginx
      build:
        context: .
        dockerfile: ./cfiles/Dockerfile-2
      ports:
        - '80:80'
      volumes:
        - ./cfiles/default.conf:/etc/nginx/conf.d/default.conf
        - ./.:/usr/share/nginx/html
      depends_on:
        - nodejs
      links:
        - nodejs

    nodejs:
      container_name: NodeJS
      build:
       context: .
       dockerfile: ./cfiles/Dockerfile-1
      command: npm start
      volumes:
       - ./.:/usr/src/app
      ports:
        - 3000

I created two Dockerfiles as follows:
Dockerfile-1:

FROM nginx:latest
COPY . /usr/share/nginx/html
COPY ./cfiles/default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD nginx -g 'daemon off;'

Dockerfile-2:

FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY ./package.json /usr/src/app/package.json
RUN npm install
RUN npm update
COPY . /usr/src/app
EXPOSE 3000

The package.json file is as follows:

{
  "name": "demo",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www",
    "test": "mocha --exit"
  },
  "dependencies": {
    "cookie-parser": "^1.4.5",
    "debug": "^4.3.1",
    "express": "^4.17.1",
    "http-errors": "^1.8.0",
    "morgan": "^1.10.0",
    "pug": "^3.0.2"
  },
  "devDependencies": {
    "mocha": "^9.0.0",
    "supertest": "^6.1.3"
  }
}

The Node.js container does not run and shows the following error:

> demo@0.0.0 start
> node ./bin/www

node:internal/modules/cjs/loader:1145
  throw err;
  ^

Error: Cannot find module 'http-errors'
Require stack:
- /usr/src/app/app.js
- /usr/src/app/bin/www
    at Module._resolveFilename (node:internal/modules/cjs/loader:1142:15)
    at Module._load (node:internal/modules/cjs/loader:983:27)
    at Module.require (node:internal/modules/cjs/loader:1230:19)
    at require (node:internal/modules/helpers:179:18)
    at Object.<anonymous> (/usr/src/app/app.js:1:19)
    at Module._compile (node:internal/modules/cjs/loader:1368:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1426:10)
    at Module.load (node:internal/modules/cjs/loader:1205:32)
    at Module._load (node:internal/modules/cjs/loader:1021:12)
    at Module.require (node:internal/modules/cjs/loader:1230:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/usr/src/app/app.js', '/usr/src/app/bin/www' ]
}

Node.js v21.7.3

I added the following line to the Dockerfile:

RUN npm ls http-errors

The result is:

#11 [nodejs 7/8] RUN npm ls http-errors
#11 1.440 demo@0.0.0 /usr/src/app
#11 1.440 +-- express@4.19.2
#11 1.440 | +-- body-parser@1.20.2
#11 1.440 | | +-- http-errors@2.0.0
#11 1.440 | | `-- raw-body@2.5.2
#11 1.440 | |   `-- http-errors@2.0.0
#11 1.440 | +-- http-errors@2.0.0
#11 1.440 | `-- send@0.18.0
#11 1.440 |   `-- http-errors@2.0.0
#11 1.440 `-- http-errors@1.8.1

What is wrong?

Cheers.

Hello,
Any idea?
What is the reason for this problem? Should I use another image?

Try docker-compose up --build. It’s possible you are running into cache issues.

1 Like