'Dockerfile: Cannot find module '/usr/src/app/dist/main' - Dist folder does not exists on container

I'm having the following error when trying to execute my NestJS App with Docker:

> [email protected] start:prod
> node dist/main

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

Error: Cannot find module '/usr/src/app/dist/main'
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

This is my Dockerfile

FROM node:16.14.2 as production

ENV NODE_ENV=production

WORKDIR /usr/src/app

COPY package*.json ./
COPY tsconfig.build.json ./
COPY tsconfig.json ./

RUN npm i -g @nestjs/cli
RUN npm i --save-dev @types/node
RUN npm install

RUN npm run build

COPY . .

#RUN ls #dist folder does not exists here

CMD ["node", "dist/main"]

However, if I run RUN ls the dist folder does not exists and the command node dist/main will not work.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source