Problem with NodeJS module

Hi,
Thanks again.
I created two Dockerfiles as follows:

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

And:

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;'

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?

Can you also post app.js please?

Also, this question might find better answers in the JavaScript forums.

1 Like

Hi,
Thanks.
The app.js is:

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

I’m confused by this pair of lines. If your working directory is /usr/src/app, and you copy a package from . to /usr/src/app… arent you… just moving the file to the same place it already is?

and also a package.json has nothing to do in a src folder…

Hi,
Thank you so much for your reply.
When you create a project from template, then the Dockerfile is as fellow:

FROM node:14-alpine
WORKDIR /usr/src/app

ARG NODE_ENV
ENV NODE_ENV $NODE_ENV

COPY package*.json /usr/src/app/
RUN npm install

COPY . /usr/src/app

ENV PORT 5000
EXPOSE $PORT
CMD [ "npm", "start" ]

Do you mean something like duplicate?

Odd. Perhaps I simply do not understand Docker well enough to interpret the file, but that looks just as redundant to me.