0

quite new to docker so I do not know where my configs are lacking, so I have a NodeJS TypeScript project that I am trying to dockerize.

The problem faced

  • When I run docker build -t server . it runs successfully building everything
  • Now when I run docker run -p 4000:5000 server I get the following error below
node:internal/modules/cjs/loader:928
  throw err;
  ^

Error: Cannot find module '/dist/server.js'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:925:15)
    at Function.Module._load (node:internal/modules/cjs/loader:769:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

So here is my Dockerfile:

FROM node

WORKDIR /usr/app

COPY package.json ./

RUN npm i

COPY . .

RUN npm run build

COPY .env ./dist/

WORKDIR /dist

EXPOSE 5000

CMD node server.js

If this will also be helpful to decipher the problem, here is also my tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "outDir": "dist",
    "baseUrl": ".",
    "moduleResolution": "Node",
    "esModuleInterop": true,
    "allowJs": true,
    "skipLibCheck": true,
    "allowSyntheticDefaultImports": true,
    "alwaysStrict": true,
    "forceConsistentCasingInFileNames": false,
    "target": "ES6",
    "strict": true,
    "noImplicitAny": true
  },
  "exclude": [
    "node_modules"
  ],
  "include": [
    "src/**/*"
  ]
}

Finally here is my package.json scripts and main:

"main": "dist/server.js",
"scripts": {
  "build": "tsc -b",
  "start": "node dist/server.js",
  "dev": "nodemon --exec ts-node src/server.ts"
},
  • Your built application will be in `/usr/app/dist`, but you're actually setting `WORKDIR /dist`. – David Maze Jan 05 '21 at 11:39
  • @DavidMaze I understand what you saying and is actually kinda making sense, but I fail to know how I can properly refactor the Dockerfile do you mind refactoring that for me please I would really appreciate that – Ntshembo Hlongwane Jan 05 '21 at 11:56
  • 1
    At the end of the Dockerfile, does changing `WORKDIR /usr/app/dist` make it work? (This is the difference between the absolute path `/dist` and the relative path `./dist` in the line above it.) – David Maze Jan 05 '21 at 12:02
  • Just changed the last ```WORKDIR``` to ```/usr/app/dist``` and I still get the same error – Ntshembo Hlongwane Jan 05 '21 at 12:17
  • Here's the github link: https://github.com/Ntshembo-Hlongwane1/House-Agency – Ntshembo Hlongwane Jan 05 '21 at 12:22

0 Answers0