为什么 Typescript 编译器在我的包之外编译?

时间:2021-07-26 15:51:56

标签: javascript typescript tsc monorepo

我有一个包含多个包和一个共享库的 monorepo:

root/
  one/
  two/
  three/
  shared-lib/
    package.json
    tsconfig.json

tsconfig.json

{
  "compilerOptions": {
    "rootDir": "src",
    "outDir": "build",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "moduleResolution": "node",
    "declaration": true,
    "declarationMap": true,
    "skipLibCheck": true,
    "target": "es5",
    "module": "esnext",
    "jsx": "react"
  },
  "include": [
    "src"
  ],
  "exclude": [
    "**/node_modules/**",
    "node_modules",
    "build"
  ]
}

当我尝试编译我的库时,我在包 onetwothree 中遇到错误。

$ cd root/shared-lib
$ yarn run tsc -p tsconfig.json

../one/some/module/up/in/here:14:25 - error TS2307: Cannot find module ...

为什么我会在这里看到这些错误?为什么 tsc 试图编译我告诉它编译的包之外的东西?

1 个答案:

答案 0 :(得分:0)

正如所建议的那样,根本原因是我无意中从 one/ 内部引用了 two/shared-lib/ 等(由于未完全将内容重构为 {{1} })。修复这些导入可以解决问题。

相关问题