简体   繁体   English

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

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

I am trying to setup a TypeScript based monorepo using Lerna where I have two packages, bar and foo .我正在尝试使用Lerna设置基于 TypeScript 的 monorepo,其中我有两个包barfoo foo imports bar by path alias and fails doing so. 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 ./tsconfig.build.json
{
  "compilerOptions": {
    "baseUrl": ".",
    "target": "es6",
    "module": "commonjs",
    "moduleResolution": "node",
    "declaration": true
  }
}
  1. ./tsconfig.json ./tsconfig.json
{
  "extends": "./tsconfig.build.json",
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@company/bar": [
        "packages/bar"
      ],
      "@company/foo": [
        "packages/foo"
      ]
    }
  }
}
  1. ./lerna.json ./lerna.json
{
  "packages": [
    "packages/*"
  ],
  "version": "0.0.0"
}
  1. ./package.json ./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"
  }
}

Package bar :套餐

  1. ./packages/bar/src/index.ts ./packages/bar/src/index.ts
export const bar = 'bar';
  1. ./packages/bar/package.json ./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 ./packages/bar/tsconfig.build.json
{
  "extends": "../../tsconfig.build.json",
  "compilerOptions": {
    "outDir": "./lib"
  },
  "include": [
    "src/**/*"
  ]
}
  1. ./packages/bar/tsconfig.json ./packages/bar/tsconfig.json
{
  "extends": "../../tsconfig.json"
}

Package foo :foo

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

console.log(bar);
  1. ./packages/foo/package.json ./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 ./packages/foo/tsconfig.build.json
{
  "extends": "../../tsconfig.build.json",
  "compilerOptions": {
    "outDir": "./lib"
  },
  "include": [
    "src/**/*"
  ]
}
  1. ./packages/foo/tsconfig.json ./packages/foo/tsconfig.json
{
  "extends": "../../tsconfig.json"
}

Finally:最后:

Running npm run tsc compiles my packages, where foo imports bar .运行npm run tsc编译我的包,其中foo导入bar It gives me the following error:它给了我以下错误:

> 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.

The error itself is pretty clear, though I do not know how to fix it (the path aliases inside ./tsconfig.json (3) look fine I guess).错误本身很清楚,但我不知道如何修复它(我猜 ./tsconfig.json (3) 中的路径别名看起来不错)。 Any ideas where my configs are messed up?我的配置搞砸了的任何想法? What point do I miss?我错过了什么?

If I change import { bar } from '@company/bar';如果我更改import { bar } from '@company/bar'; to import { bar } from '../../bar/src'; import { bar } from '../../bar/src'; everything works fine, however I would like to stick with the first way to import bar .一切正常,但是我想坚持使用第一种导入bar

This is the part that allows you to import the packages as @company/bar这是允许您将包导入为@company/bar

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

This is specified in ./tsconfig.json .这在./tsconfig.json指定。 When you try to compile package foo it is using ./packages/foo/tsconfig.build.json which never includes the root config or specify the paths itself, you probably want to remove the import scheme from the base tscofig and add them to the inner package configs:当您尝试编译foo包时,它使用./packages/foo/tsconfig.build.json ,它从不包含根配置或指定路径本身,您可能希望从基础tscofig删除导入方案并将它们添加到内包配置:

./packages/foo/tsconfig.build.json should incorporate this: ./packages/foo/tsconfig.build.json应该包含以下内容:

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

and similarly packages/bar/tsconfig.build.json should be allowed to import @company/foo if needed.同样,如果需要,应该允许packages/bar/tsconfig.build.json导入@company/foo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM