【问题标题】:Typescript compiler not solving Path Mapping modules打字稿编译器不解决路径映射模块
【发布时间】:2018-11-22 11:51:29
【问题描述】:

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

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

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

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

ts-node 的正确配置是什么

这是我的 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】:

    问题在于 typescript 编译器的调用。如果您使用特定的导入文件调用tsc,它是ignores a tsconfig.json in the same directory

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-09
      • 2022-01-05
      • 2018-07-30
      • 2018-03-11
      • 2018-06-03
      • 2023-03-24
      • 2016-05-08
      • 2022-01-25
      相关资源
      最近更新 更多