【问题标题】:Changing dist directory in Nestjs在 Nestjs 中更改 dist 目录
【发布时间】:2020-04-23 09:05:21
【问题描述】:

我正在构建一个基于 Nestjs 的 API。 main.ts 文件嵌套在我的 src 目录的子文件夹中。结果,编译后的文件也嵌套在dist目录中,我的入口点是dist/api/main.js而不是dist/main.js。我想保持这种状态。

使用npm run start:dev 启动我的应用程序时,我收到以下错误:

4:47:32 PM - Found 0 errors. Watching for file changes.
internal/modules/cjs/loader.js:969
    throw err;
    ^

Error: Cannot find module '/home/ymonb/Nest.js/myProject/dist/main'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:966:17)
    at Function.Module._load (internal/modules/cjs/loader.js:859:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

我需要能够使用新路径重新配置 Nest。我查看了the documentation,但没有看到任何指定不同入口点的内容。

我想也许我需要编辑nest-cli.json,但我在这个文件中找不到任何关于可能选项的文档(如果你知道这样的文档,我很感兴趣!)。

这是我的nest-cli.json

{
  "collection": "@nestjs/schematics",
  "sourceRoot": "src/api"
}

有什么想法吗?

【问题讨论】:

  • 问题解决了吗?

标签: nestjs


【解决方案1】:

您可以像这样编辑nest-cli.json:

{
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "entryFile": "your/main/js/path"
}

nest-cli 将加载:your/main/js/path.js

我在源代码中找到了配置。 nest-cli/lib/configuration/configuration.ts

【讨论】:

    【解决方案2】:

    转到您的“nest-cli.json”并编辑“entryFile”配置。
    它应该链接到“dist/main”文件中的 main.js 文件。
    它应该是这样的:

    {
      "collection": "@nestjs/schematics",
      "sourceRoot": "src",
      "entryFile": "copy the relative path of your main.js and paste it here"
    }
    

    【讨论】:

      【解决方案3】:

      这些是打字稿构建配置。 将tsconfig.json 文件中的rootDir 条目更改为src。 这样做会将 src 下的结构复制到 dist。

      【讨论】:

      • 谢谢,我试过了,但没用。我不确定它是否与 Typescript 有关,因为编译工作正常。事实上,npm run build 运行良好。问题似乎是 Nest 在 dist 目录中找不到文件。这就是为什么我认为 Nest 配置是错误的,而不是 Typescript。我在Github 上放了一些代码来重现这个问题。只需npm installnpm run start:dev
      • 确保tsconfig.json 中的outDir 指向dist 而不是dist/api,并且在nest-cli.json 中将"sourceRoot": "src/api" 更改为"sourceRoot": "src"。试试这些。
      猜你喜欢
      • 2014-03-28
      • 1970-01-01
      • 2020-04-15
      • 1970-01-01
      • 2021-10-02
      • 1970-01-01
      • 2019-12-15
      • 2014-08-09
      • 2016-03-30
      相关资源
      最近更新 更多