gpt4 book ai didi

typescript - 找不到模块 - typescript 路径别名错误

转载 作者:行者123 更新时间:2023-12-04 16:28:06 25 4
gpt4 key购买 nike

在编译时,我收到无法修复的错误:/

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"
]
}
}

后端/index.ts:
import 'module-alias/register'
import { x } from '@shared'

console.log(x)

共享/index.ts:
export const x = 'this is shared index'

最佳答案

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

tsc --traceResolution

正如我发现 '@shared' 被用作文件夹名称,所以它看起来像:
Resolving module name '@shared' relative to base url 'D:/apps/my-app/src' - 'D:/apps/my-app/src/@shared'.

在我将别名更改为“共享”并设置 baseUrl 后,一切都开始工作:
{
"compilerOptions": {
"moduleResolution": "node",
"baseUrl": "../",
"outDir": "../../build/backend"
},
"extends": "../../configs/tsconfig.base.json",
"references": [
{
"path": "../shared"
}
],
"paths": { "shared": ["shared/index"] }
}

关于typescript - 找不到模块 - typescript 路径别名错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58809944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com