我有一个具有以下结构的 NX monorepo:
apps/
├── api/
│ ├── src/
│ ├── tsconfig.json
│ └── tsconfig.lib.json
└── client/
├── src
├── tsconfig.json
└── tsconfig.lib.json
libs/
├── api/
│ ├── src/
│ ├── tsconfig.json
│ └── tsconfig.lib.json
├── ui/
│ ├── src/
│ │ ├── button/
│ │ │ ├── index.ts
│ │ │ └── Button.tsx
│ │ └── ...
│ ├── tsconfig.json
│ └── tsconfig.lib.json
└── shared/
└── ...
tsconfig.base.json
tsconfig.base.json
{
"compilerOptions": {
...
"rootDir": ".",
"baseUrl": ".",
"paths": {
"@name/api": ["libs/api/src/index.ts"],
"@name/ui/*": [
"libs/ui/src/*"
],
},
"exclude": ["tmp"]
}
tsconfig.json
{
"compilerOptions": {
...
},
"references": [
{
"path": "./tsconfig.lib.json"
}
],
"extends": "../../tsconfig.base.json"
}
tsconfig.lib.json
{
"extends": "./tsconfig.json",
"compilerOptions": {
...
},
"include": ["src/**/*.js", "src/**/*.jsx", "src/**/*.ts", "src/**/*.tsx"]
}
如果需要暴露给其他项目,所有组件都通过index.ts导出。
导入就像
@name/ui/folder
一样完成。每个文件夹都有index.ts 桶文件。代码运行完美,但是导入该项目的 tsconfig 的项目不断抱怨。
TS 不断抱怨 @name/ui/* 的路径多次给出此错误:
File '/workspace/libs/ui/src/*/index.ts' not found.
The file is in the program because:
Root file specified for compilation
我做了一些基础研究,我认为在通配符后面放置文件是有效的,并且代码确实有效。我只是希望我能摆脱这个特定的错误。如有任何帮助,我们将不胜感激!
原始来源:https://www.reddit.com/r/typescript/comments/138rvsn/ts_keeps_ throwing_file_not_found_root_file/
是的,我重新启动了 IDE Typescript 服务器等。一旦我开始编辑文件,它就会回来。我删除了路径中的通配符,它就消失了,但我需要用通配符来支持它。
请帮忙
代码运行完美,但是导入该项目的 tsconfig 的项目一直在抱怨
除非我弄错了,否则打字稿必须编译才能运行代码。我猜您在 IDE/编辑器中看到了错误,但如果您直接运行代码,它就可以正常工作。
考虑到这个假设,您使用 VSCode 吗?如果是,我想到了 2 个想法:
我个人在 VSCode IDE 读取 tsconfig.json 时遇到了一些奇怪的问题,有时我不得不仅为 vscode 修改 tsconfig。您可以在整个文件夹结构的根目录中指定
tsconfig.json
,并删除仅适用于 vscode 的 files
、include
和 exclude
参数(参见此处)。您不会使用此文件来构建;它仅适用于 IDE。
VSCode 可以运行与代码库中不同版本的 Typescript 编译器。请参阅 VSCode 文档 和 StackOverflow 帖子。也许尝试确保 IDE 使用相同版本的 Typescript?
希望这有帮助!