1

I'm currently in the process of containerizing my App. Part of the App is a NodeJS Express Backend. It works without any issues when I run it on my console with node index.js or nodemon index.js.

enter image description here

Inside the middlewares/index.js I do import authJwt.js and verifySignup.js via require('./verifySignup) and require('./authJwt).

This works unsurprisingly well when I run my app from the console. However If i build my docker-image using the following Dockerfile-Code:

# build environment
FROM node:14

# Working Directoy
WORKDIR /app

ENV FILE_UPLOADS="./files/uploads"
ENV FILE_UPLOADS_PART="./files/uploads_part"
ENV PORT=3001
ENV DB_HOST="localhost"
ENV DB_USER="root"
ENV DB_PASSDWORD="root_password"
ENV DB_NAME="upload_db"
ENV SECRET_KEY="bezkoder-secret-key"

# Copy Needed Files for Dependencies
COPY package.json /app/package.json
COPY yarn.lock /app/yarn.lock

# Install with YARN
RUN yarn install

# Copy Source Files
COPY ./ /app

EXPOSE 3001

CMD [ "yarn", "start" ]

When I run the Image it immediately terminates. Looking at the logs I find the following error:

$ docker logs 031241ce227a
yarn run v1.22.5
$ node index.js
internal/modules/cjs/loader.js:895
  throw err;
  ^

Error: Cannot find module './verifySignUp'
Require stack:
- /app/middleware/middleware.js
- /app/routes/auth.routes.js
- /app/index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:892:15)
    at Function.Module._load (internal/modules/cjs/loader.js:742:27)
    at Module.require (internal/modules/cjs/loader.js:964:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/app/middleware/middleware.js:2:22)
    at Module._compile (internal/modules/cjs/loader.js:1075:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1096:10)
    at Module.load (internal/modules/cjs/loader.js:940:32)
    at Function.Module._load (internal/modules/cjs/loader.js:781:14)
    at Module.require (internal/modules/cjs/loader.js:964:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/app/middleware/middleware.js',
    '/app/routes/auth.routes.js',
    '/app/index.js'
  ]
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I then exported my docker-image using docker export adoring_kowalevski > contents.tar to look at the filesystem inside the image, but everything is where it should be.

In a desperate attempt I hoped, that the issue might occur because I have multiple index.js in different folders, so I changed the names of all except the one in the root directory, but issue persisted. This is why in the error-log there is a middleware/middleware.js, it was after renaming the middleware/index.js.

Edit: I tried using rfr to change my references from require('../middleware') to rfr('middleware'). It still workds from console but still not inside the container. So it seems that the issue doesn't occur because of the relative file paths.

2
  • if that is your structure "Inside the middlewares/index.js I do import authJwt.js and verifySignup.js via require('./verifySignup) and require('./authJwt)." you should use '../verifySignup' Commented Sep 1, 2020 at 19:42
  • Can you post your package.json too? Commented Sep 1, 2020 at 19:49

1 Answer 1

1

Turns out I'm stupid (somewhat).

The import inside middleware/index.js looked like this require('./verifySignUp'). If we look at the file verifySignup.js inside the middleware Folder we can see, that it is named without the capital 'U'.

Why did it still work from console? Inside verifySignup.js is the following piece of code:

module.exports = {
    authJwt,
    verifySignUp
};

Here it is written with capital 'U'. This means that running node index.js from the console only cared about whatever was written in the module.exports and not the actual Filename, however when running inside Docker, the Filename matters.

Renaming the file from verifySignup.js to verifySignUp.js solved my problems.

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

1 Comment

That didference comes from os. inside the docker in unix file names differ with case but in windows not.

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.