带有 Lerna 和 TypeScript 的 Monorepo 无法通过路径别名导入包

Monorepo with Lerna and TypeScript fails to import package by path alias

我正在尝试使用 Lerna 设置基于 TypeScript 的 monorepo,其中我有两个包,barfoofoo 通过路径别名导入 bar 但失败了。

  1. tree
.
├── lerna.json
├── package.json
├── package-lock.json
├── packages
│   ├── bar
│   │   ├── lib
│   │   │   ├── index.d.ts
│   │   │   └── index.js
│   │   ├── package.json
│   │   ├── src
│   │   │   └── index.ts
│   │   ├── tsconfig.build.json
│   │   └── tsconfig.json
│   └── foo
│       ├── lib
│       │   ├── index.d.ts
│       │   └── index.js
│       ├── package.json
│       ├── src
│       │   └── index.ts
│       ├── tsconfig.build.json
│       └── tsconfig.json
├── tsconfig.build.json
└── tsconfig.json
  1. ./tsconfig.build.json
{
  "compilerOptions": {
    "baseUrl": ".",
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "declaration": true
  }
}
  1. ./tsconfig.json
{
  "extends": "./tsconfig.build.json",
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@company/bar": [
        "packages/bar"
      ],
      "@company/foo": [
        "packages/foo"
      ]
    }
  }
}
  1. ./lerna.json
{
  "packages": [
    "packages/*"
  ],
  "version": "0.0.0"
}
  1. ./package.json
{
  "name": "root",
  "private": true,
  "scripts": {
    "tsc": "lerna run tsc"
  },
  "devDependencies": {
    "lerna": "^3.22.1",
    "ts-node": "^9.0.0",
    "ts-node-dev": "^1.0.0-pre.63",
    "typescript": "^4.0.3"
  }
}

套餐:

  1. ./packages/bar/src/index.ts
export const bar = 'bar';
  1. ./packages/bar/package.json
{
  "name": "@company/bar",
  "version": "1.0.0",

  ...

  "main": "lib/index.js",
  "types": "lib/index.d.ts",
  "scripts": {
    "tsc": "tsc -p tsconfig.build.json"
  }
}
  1. ./packages/bar/tsconfig.build.json
{
  "extends": "../../tsconfig.build.json",
  "compilerOptions": {
    "outDir": "./lib"
  },
  "include": [
    "src/**/*"
  ]
}
  1. ./packages/bar/tsconfig.json
{
  "extends": "../../tsconfig.json"
}

foo:

  1. ./packages/foo/src/index.ts
import { bar } from '@company/bar';

console.log(bar);
  1. ./packages/foo/package.json
{
  "name": "@company/foo",
  "version": "1.0.0",

  ...

  "main": "lib/index.js",
  "types": "lib/index.d.ts",
  "scripts": {
    "tsc": "tsc -p tsconfig.build.json"
  }
}
  1. ./packages/foo/tsconfig.build.json
{
  "extends": "../../tsconfig.build.json",
  "compilerOptions": {
    "outDir": "./lib"
  },
  "include": [
    "src/**/*"
  ]
}
  1. ./packages/foo/tsconfig.json
{
  "extends": "../../tsconfig.json"
}

最后:

运行 npm run tsc 编译我的包,其中 foo 导入 bar。它给了我以下错误:

> lerna run tsc

lerna notice cli v3.22.1
lerna info Executing command in 2 packages: "npm run tsc"
lerna info run Ran npm script 'tsc' in '@company/bar' in 2.4s:

> @company/bar@1.0.0 tsc /.../monorepo-lerna/packages/bar
> tsc -p tsconfig.build.json

lerna ERR! npm run tsc exited 2 in '@company/foo'
lerna ERR! npm run tsc stdout:

> @company/foo@1.0.0 tsc /.../monorepo-lerna/packages/foo
> tsc -p tsconfig.build.json

src/index.ts(1,21): error TS2307: Cannot find module '@company/bar' or its corresponding type declarations.

错误本身很清楚,尽管我不知道如何修复它(./tsconfig.json (3) 中的路径别名我猜看起来不错)。我的配置搞砸了的任何想法?我错过了什么?

如果我将 import { bar } from '@company/bar'; 更改为 import { bar } from '../../bar/src'; 一切正常,但是我想坚持使用第一种方法导入 bar.

这是允许您将包导入为 @company/bar

的部分
    "paths": {
      "@company/bar": [
        "packages/bar"
      ],
      "@company/foo": [
        "packages/foo"
      ]
    }

这在 ./tsconfig.json 中指定。当您尝试编译包 foo 时,它使用的 ./packages/foo/tsconfig.build.json 从不包含根配置或指定路径本身,您可能想从基础 tscofig 中删除导入方案并添加它们到内包配置:

./packages/foo/tsconfig.build.json 应该包含这个:

{
  "compilerOptions": {
    "paths": {
      "@company/bar": [
        "../bar"
      ]
    }
  }
}

并且类似地,如果需要,应该允许 packages/bar/tsconfig.build.json 导入 @company/foo