简体   繁体   中英

docker unable to find the main.js file for running the backend in nestjs with typeorm

docker-compose.yml

version: '3.7'
services: 
    backend:
        container_name: backend
        build:
            context: backend
            dockerfile: Dockerfile
        ports: 
            -   8000:8000
        volumes:
            -   ./backend:/app
        depends_on: 
            -   pgdb
        
    pgdb:
        container_name: pgdb
        image: postgres
        environment: 
            POSTGRES_PASSWORD: 'admin'
            POSTGRES_USER: 'postgres'
            POSTGRES_DB: 'ambassador'
        ports: 
            -   5432:5432
        volumes:
        - db-data:/var/lib/postgresql

volumes: 
    db-data:

Dockerfile

FROM node:16 
WORKDIR /app
COPY package.json .
COPY yarn.lock .
RUN yarn install
COPY . .
RUN yarn build
EXPOSE 8000
CMD ["yarn", "start:prod"]

backend -> src -> main.ts

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const configService: ConfigService = app.get<ConfigService>(ConfigService);
  const port = configService.get('PORT');    //PORT=8000 in .env
  await app.listen(port);
  Logger.log(`Listening on http://localhost:${port}`);
}
bootstrap();

I keep getting the error

Error: Cannot find module '/app/dist/main'

Please correct me if I am wrong here. As per Dockerfile instructions, I added a new working directory within the container called "app", copied the package.json and lock file to app and ran install to get the node_modules. Then copied everything from backend directory to app directory in docker. Running yarn build after that would create a dist folder which would have a main.js file which I need to reach. Yet I get that error. I ran yarn build on my local machine and saw that dist has ormconfig files and src subdirectory which contains main.js. Hence, I changed value of "start:prod" in package.json to "node dist/src/main" but even that shows

Error: Cannot find module '/app/dist/src/main'

please enlighten me as to where or where all am I wrong here?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM