Typescript编译器无法解析Path Mapping模块

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

我想使用ts-node和.ts文件一样使用path mapping,如paths: { "@/*": ["src/*"] }

并导入像import Sample from "@/sample"而不是import Sample from "../../src/sample"

当编译tsc index.js时,我得到了这个:

index.ts:1:18 - 错误TS2307:找不到模块'@ / sample。

什么是ts节点的正确配置

这是我的tsconfig,json文件:

  {
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "strict": true,
    "baseUrl": "./",
    "paths": { "@/*": ["src/*"] },
    "esModuleInterop": true
  }
}

和我的示例项目:https://github.com/daniel-dia/ts-path-mapping

typescript tsc
1个回答
0
投票

问题是调用typescript编译器。如果你用特定的导入文件调用tsc,那就是ignores a tsconfig.json in the same directory

在命令行中指定输入文件时,将忽略tsconfig.json文件。

只需将您的构建脚本更改为"build": "tsc"并将"files": ["index.ts"]添加到您的tsconfig.json

© www.soinside.com 2019 - 2024. All rights reserved.