Nestjs 创建工作区后找不到构建源

问题描述 投票:0回答:1

我将 NestJS 项目目录重命名为

server
,并将
docker-compose
tsconfig.json
package.json
文件移到此文件夹之外。

在主目录(以项目命名)内,我在

package.json
文件中定义了一个工作区来访问不同的命名空间。

当我使用

npm run start
命令运行 Docker 文件时,会在主目录(
dist
文件夹之外)中创建一个
server
文件夹,其结构如下:

但是,

server
文件夹内的项目无法运行,并且出现以下错误:

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

通过docker运行项目的命令:

npm run start

主目录中
package.json
的内容:

{
  "name": "auction",
  …
  "workspaces": [
    "auction-client",
    "auction-server",
    "shared"
  ],
  "scripts": {
    …
    "docker:compose": "docker compose up",
    "server:dev": "wait-on tcp:6379 && npm run start:dev --workspace=auction-server",
    "start": "concurrently 'npm:docker:compose' 'npm:server:dev'",
    …
  },
  "devDependencies": {
    "concurrently": "^8.2.2",
    "prettier": "^2.8.8",
    "wait-on": "^6.0.1"
  },
  "dependencies": {
    "source-map-support": "^0.5.21"
  }
}

docker-compose.yml
的内容:

services:
  app:
    container_name: ${APP_CONTAINER_NAME}-app
    build:
      context: ./docker
      dockerfile: ./Dockerfile
    image: auction-project:alpine-10.0.5
    #restart: always
    env_file:
      - ./.env
    networks:
      - default
    ports:
      - '80:3000'
    volumes:
      - .:/opt
      - ./docker/storage:/home/storage:rw
    working_dir: /opt
    command: npm run start:dev
    depends_on:
      - database
  database:
    …
  phpmyadmin:
    ...
  redis:
    …
networks:
  …

Dockerfile
文件夹内
docker
的内容:

FROM node:19.9.0-alpine
RUN corepack disable yarn
RUN npm i -g yarn
RUN npm i -g @nestjs/[email protected]

WORKDIR /opt

主目录中
tsconfig.json
的内容(
docker-compose.yml
旁边):

{
    "compilerOptions": {
        "module": "commonjs",
        "declaration": true,
        "removeComments": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "allowSyntheticDefaultImports": true,
        "target": "ES2023",
        "sourceMap": true,
        "outDir": "./dist",
        "baseUrl": "./",
        "incremental": true,
        "skipLibCheck": true,
        "strictNullChecks": false,
        "noImplicitAny": false,
        "strictBindCallApply": false,
        "forceConsistentCasingInFileNames": false,
        "noFallthroughCasesInSwitch": false
    }
}

package.json
文件夹内
server
的内容:

{
  "name": "auction-server",
  "version": "0.0.1",
  "description": "<p></p>",
  "author": "",
  "private": true,
  "license": "COMMERCIAL",
  ...
  "main": ".eslintrc.js",
  "directories": {
  },
  "keywords": []
}

tsconfig.json
文件夹内
server
的内容:

{
    "compilerOptions": {
        "types": ["node"],
        "module": "commonjs",
        "declaration": true,
        "removeComments": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "allowSyntheticDefaultImports": true,
        "target": "ES2023",
        "sourceMap": true,
        "outDir": "./dist",
        "baseUrl": "./",
        "incremental": true,
        "skipLibCheck": true,
        "strictNullChecks": false,
        "noImplicitAny": false,
        "strictBindCallApply": false,
        "forceConsistentCasingInFileNames": false,
        "noFallthroughCasesInSwitch": false,
    }
}
node.js docker docker-compose nestjs
1个回答
0
投票

尝试更改命令:

node dist/main.js

如果使用 Dockerfile 构建,请使用

CMD ["node", "dist/main.js"]
© www.soinside.com 2019 - 2024. All rights reserved.