代码之家  ›  专栏  ›  技术社区  ›  Daniel Santos

Typescrpit编译器不解决路径映射模块

  •  1
  • Daniel Santos  · 技术社区  · 7 年前

    我想使用ts节点 .ts 使用 path mapping 喜欢 paths: { "@/*": ["src/*"] }

    像进口一样 import Sample from "@/sample" 而不是 import Sample from "../../src/sample"

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

    指数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

    1 回复  |  直到 5 年前
        1
  •  0
  •   Johannes Reuter    7 年前

    问题是typescript编译器的调用。如果你打电话 tsc 通过一个特定的导入文件 ignores a tsconfig.json in the same directory :

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

    只需将构建脚本更改为 "build": "tsc" 加上 "files": ["index.ts"] 给你的 tsconfig。json .

    推荐文章