错误 TS6059:文件不在“rootDir”下..“rootDir”预计包含所有源文件

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

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

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

我的 PWD 是

/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
5个回答
70
投票

什么是
rootDir

rootDir
设置为根文件夹,其中包含所有源文件。如果未指定,TS 将自动为所有输入选择合适的父文件夹。
rootDir
确定输出目录

这个错误是什么意思?

我的猜测是你在

import
的某个地方有一个
logging.ts
notifier-server
声明:

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

然后

logging.ts
模块将被编译器自动包含,无论
include
中的
exclude
tsconfig.json
选项如何。检查所有包含的文件的一种方法是
tsc --listFiles

tsconfig.json
外部的
notifier-server
文件在这里没有帮助。编译器在每次
tsc
编译时仅选取一个配置,并可选择提取 继承的配置。如果在
notifier-server
项目根目录(您开始
tsc
的位置)中找不到,只有编译器向上搜索父目录链,直到找到配置。

可能的解决方案

一个修复方法是从编译器选项中删除

"rootDir": "src"
,这样它就会自动设置。注意:
rootDir
随后会将这两个项目视为输入!

替代方案:您可以添加

logging.ts
项目中包含的单独
notifier-server/src
模块并删除外部
import

希望有帮助!


5
投票

我通过在

paths
中复制
tsconfig
来得到此错误。 删除后者解决了问题:

// ./tsConfig.json
"paths": {
  "@myPackage": ["./myPackage/index.ts"],
},

// ./MY_OTHER_PACKAGE/tsConfig.json
"paths": {
 "@myPackage": ["../myPackage/index.ts"]
},

0
投票
 -src
     index.ts
 -tsconfig.json

如果在 tsconfig.json 中,其

"rootDir": "./src"
index.ts
位于 root 中,则不起作用,请确保
index.ts
位于
src


0
投票

我的解决方案是针对角度库的。对于外部库,我将

paths
定义添加到
tsconfig.lib.json
文件中。

tsconfig.lib.json

{
  "extends": "../../../tsconfig.json",
  "compilerOptions": {
    "resolveJsonModule": true,
    "outDir": "../../../out-tsc/lib",
    "declaration": true,
    "declarationMap": true,
    "inlineSources": true,
    "types": [],
    "paths": {
      "@tbtk/popover": ["dist/tbtk/popover"]
    }
  },
  "exclude": ["**/*.spec.ts"]
}

0
投票

可能出现的问题

1-循环依赖

2- 包名称应与定义的路径相同。

// libs/utils/package.json 
{
...
"name":"Should-Be-Same"
...
}



// tsconfig.json
{ 
...

"Should-Be-Same":["/libs/utils/lib/src/index.ts"]

...
}

3-路径中的拼写错误

© www.soinside.com 2019 - 2024. All rights reserved.