网站搜索

使用 Lerna 和 TypeScript 的单仓库无法通过路径别名导入包


我正在尝试使用 Lerna 设置一个基于 TypeScript 的 monorepo,其中我有两个包,bar 和 foo。foo 通过路径别名导入 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"
  }
}

包 bar:

  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/[email  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/[email  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。

答案#1

这是允许你以 @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。

相关文章