具有来自Typescript路径的Monorepo无法正常工作

时间:2019-10-25 08:18:55

标签: javascript typescript tsconfig monorepo yarn-workspaces

我有具有以下文件结构的monorepo(纱线工作区):

├── client                (workspace @client)
│   ├── package.json
│   └── tsconfig.json     (extended tsconfig)
├── server                (workspace @server)
│   ├── getData.ts
│   ├── package.json
│   └── tsconfig.json     (extended tsconfig)
├── shared
│   └── sanitizeData.ts
├── package.json          (monorepo root)
└── tsconfig.json         (base tsconfig)

我想使用shared/sanitizeData.ts中的server/getData.ts中的函数

我尝试使用Typescript中的paths,根据文档来看,它看起来非常简单,但是我做错了事:

error TS2307: Cannot find module '@shared/sanitizeData'.

server/tsconfig.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "baseUrl": ".",
    "rootDir": "../",
    "outDir": "build",
    "paths": {
      "@shared/*": ["shared/*"]
    }
  }
}

server/getData.js

import { sanitizeData } from "@shared/sanitizeData";

能帮我吗?

1 个答案:

答案 0 :(得分:3)

路径是相对于baseUrl的,因此在您的情况下,您必须将["shared/*"]替换为["../shared/*"]