找不到模块-打字稿路径别名错误

时间:2019-11-11 23:21:48

标签: typescript

在编译时出现无法修复的错误:/

index.ts:2:19 - error TS2307: Cannot find module '@shared'

有什么想法吗?

项目结构:

enter image description here

tsconfig.json:

{
  "compilerOptions": {
    "outDir": "../../build/backend",
  },
  "extends": "../../configs/tsconfig.base.json",
  "references": [
    {
      "path": "../shared"
    }
  ],
  "paths": {
    "@shared": [
      "../shared/index"
    ]
  }
}

backend / index.ts:

import 'module-alias/register'
import { x } from '@shared'

console.log(x)

shared / index.ts:

export const x = 'this is shared index'

1 个答案:

答案 0 :(得分:0)

您可以使用此命令来跟踪问题:

tsc --traceResolution

我发现'@shared'用作文件夹名称,因此它看起来像:

Resolving module name '@shared' relative to base url 'D:/apps/my-app/src' - 'D:/apps/my-app/src/@shared'.

在我将别名更改为“ shared”并设置baseUrl之后,一切开始起作用:

{
  "compilerOptions": {
    "moduleResolution": "node",
    "baseUrl": "../",
    "outDir": "../../build/backend"
  },
  "extends": "../../configs/tsconfig.base.json",
  "references": [
    {
      "path": "../shared"
    }
  ],
  "paths": { "shared": ["shared/index"] }
}