我想使用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
}
}
问题是调用typescript编译器。如果你用特定的导入文件调用tsc
,那就是ignores a tsconfig.json
in the same directory:
在命令行中指定输入文件时,将忽略tsconfig.json文件。
只需将您的构建脚本更改为"build": "tsc"
并将"files": ["index.ts"]
添加到您的tsconfig.json
。