在 tsconfig 中定义的路径中找不到模块

问题描述 投票:0回答:1

这让我发疯,我错过了一些东西,但我不知道它是什么。

鉴于以下进口:

import { SOME_SCHEMA } from '../../../schema/src/schemas/someschema';
import { SomeType } from '../../../someComponent/src/types/someType';

我想通过向

tsconfig
添加别名来缩短路径,如下所示:

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "paths": {
      "schema/*": ["../schema/src/*"],
      "someComponent/*": ["../someComponent/src/*"]
    }
  }
}

然后我像这样导入它们:

import { SOME_SCHEMA } from 'schema/schemas/clientTaskSchema';
import { SomeType } from 'someComponent/types/someType';

我的 IDE 不会抱怨并且可以找到这两个定义。但是,当我构建时,我仅收到

SomeType
的错误(而不是
SOME_SCHEMA
的错误。

找不到模块“someComponent/types/someType”或其相应的类型声明。

17 从 'someComponent/types/someType' 导入 { SomeType };

typescript tsconfig
1个回答
0
投票

来自官方打字稿文档这里

请注意,此功能不会更改 tsc 发出导入路径的方式,因此路径只能用于通知 TypeScript 另一个工具具有此映射,并将在运行时或捆绑时使用它。

话虽如此,我建议使用像 tsconfig-paths 这样的工具在运行时加载其位置在 tsconfig.json 的 paths 部分中指定的模块。

因此,您不必运行

tsc

 将打字稿编译为 javascript,而是可以使用以下命令:

tsc && node -r tsconfig-paths/register dist/${yourStartScript}.js
    
© www.soinside.com 2019 - 2024. All rights reserved.