【问题标题】:Wrong build in dist/ folder when server imports module from external folder当服务器从外部文件夹导入模块时,在 dist/ 文件夹中构建错误
【发布时间】:2019-10-08 23:22:25
【问题描述】:

我正在使用来自 nestjs (https://github.com/nestjs/nest/tree/master/sample/03-microservices) 的微服务混合示例来熟悉基础知识。它目前使用位于 src 文件夹 (src/math) 内的微服务。我想将微服务移动到微服务文件夹 (microservices/math/...) 下的根文件夹中,以便可以在此结构中构建其他微服务。

当我使用 "start:prod": "node dist/main.js" 运行它时,如果我在 app.module.ts 中导入的 math.module 是示例中的那个,在 './math/ math.module' 它工作正常。如果我将数学文件夹内容复制到根目录的微服务文件夹中,并从“../microservices/math.module”中引用math.module,那么我所拥有的dist结构是错误的:

  • 分布
    • 微服务
      • 常见
      • 数学
      • app.module.d.ts
      • main.d.ts

当然,在这种情况下,它会尝试在“dist”中运行 main.js,但它不再存在,而是自动放入 src 中,而不是位于 dist 文件夹的根目录中。

这纯粹是我需要调整的打字稿配置吗?

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "noImplicitAny": false,
    "removeComments": true,
    "noLib": false,
    "allowSyntheticDefaultImports": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es6",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./"
  },
  "exclude": ["node_modules"]
}

tsconfig.build.json

{
  "extends": "./tsconfig.json",
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}

【问题讨论】:

  • 你是在用 tsc 命令编译你的项目吗?这可能是您的 tsconfig.json 文件中的配置问题...粘贴它。
  • @KevinMansel 我用 tsconfigs 编辑了答案。另外这个例子是为了举例说明我在另一个项目中遇到的问题,这个问题更详细地分享(nestjs with ssr、混合微服务设置、角度等)。
  • @KevinMansel 你有什么想法吗?

标签: node.js typescript nestjs tsc tsconfig


【解决方案1】:

这里的问题是,当您从文件夹中importing 本地代码时,这不是用于构建的tsconfig.json 中的included。在这种情况下,打字稿将使用代码在最不深的文件夹的父文件夹中进行,并构建该文件夹树的结构,因为构建的文件应该具有彼此相同的相对路径。

【讨论】:

  • 所以我这里的设置基本上不能工作?没什么大不了的,我可以改变结构,只是为了知道所以我不会浪费时间。
  • @M_M 实际上,它应该可以工作,它只会更改/dist 文件夹下的文件夹结构,您应该在运行脚本中更改构建目标文件的路径
猜你喜欢
  • 2020-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-19
  • 1970-01-01
  • 2014-01-08
相关资源
最近更新 更多