【问题标题】:error TS6059: File is not under 'rootDir' .. 'rootDir' is expected to contain all source files错误 TS6059:文件不在“rootDir”下。“rootDir”应包含所有源文件
【发布时间】:2019-12-16 17:55:44
【问题描述】:

我收到了这个相当无意义的 tsc 转译错误:

错误 TS6059:文件 '/Users/alex/codes/interos/teros-cli/src/logging.ts' 不在 'rootDir' '/Users/alex/codes/teros/notifier-server/src'。 '根目录' 预计将包含所有源文件。

我的密码是/Users/alex/codes/teros/notifier-server/Users/alex/codes/teros/notifier-server/tsconfig.json 的 tsconfig.json 文件是:

{
  "compilerOptions": {
    "outDir": "dist",
    "allowJs": false,
    "pretty": true,
    "resolveJsonModule": true,
    "sourceMap": false,
    "skipLibCheck": true,
    "rootDir": "src",
    "declaration": false,
    "baseUrl": ".",
    "target": "es2018",
    "module": "commonjs",
    "noImplicitAny": true,
    "removeComments": true,
    "allowUnreachableCode": true,
    "lib": [
      "es2017",
      "es2018"
    ]
  },
  "compileOnSave": false,
  "include": [
    "src"
  ]
}

这似乎是一个错误..因为 teros-cli 目录在 PWD 之外,并且由单独的 tsconfig.json 文件管理。

我什至将此字段更改为:

  "include": [
    "/Users/alex/codes/teros/notifier-server/src"
  ],
  "exclude": [
    "/Users/alex/codes/teros/teros-cli"
  ]

还是同样的错误。

【问题讨论】:

    标签: typescript tsc


    【解决方案1】:

    什么是rootDir

    rootDir 设置为根文件夹,其中包含所有您的源文件。如果未指定,TS 将自动为所有输入选择合适的父文件夹。 rootDir 还有determines the output directory

    错误是什么意思?

    我猜你在notifier-server 某处有一个import 声明logging.ts

    import {logger} from "@teros-cli/logging" // or similar
    

    那么logging.ts模块将被编译器automatically included,不管tsconfig.json中的includeexclude选项。检查所有包含文件的一种方法是tsc --listFiles

    notifier-server 之外的 tsconfig.json 文件在这里没有帮助。编译器在每次 tsc 编译时只选择一个配置,并可选择提取 inherited configs。如果它在notifier-server 项目根目录(你开始tsc)中找不到一个,只有编译器searches upwards the parent directory chain,直到找到一个配置。

    可能的解决方案

    一种解决方法是从编译器选项中删除"rootDir": "src",这样它就会自动设置。注意:rootDir 会将这两个项目都视为输入!

    替代方案:您可以在notifier-server/src 项目中添加一个单独的logging.ts 模块并删除外部import

    希望,这会有所帮助!

    【讨论】:

    • 另一种选择是使用项目参考。请参阅this answer 处理rootDir 问题并使用项目参考来管理内部依赖关系。事情将开始变得更有意义。
    猜你喜欢
    • 2021-09-16
    • 2018-06-19
    • 1970-01-01
    • 2019-01-21
    • 2018-12-06
    • 1970-01-01
    • 2020-07-10
    • 2019-04-12
    • 1970-01-01
    相关资源
    最近更新 更多