node.js - 如何在 TypeScript 中设置绝对模块导入?
启用绝对导入路径的“最佳”方法是什么,即
import { my_module } from 'src/my_module_file';
关于这一点有矛盾的信息;有些人声称可以通过在 tsconfig.json 的 compilerOptions 中设置 baseUrl 和 paths 来实现,有些人则持相反观点。作为 TypeScript 新手,我有些困惑。
我是否需要使用 resolve.modules 设置来配置 Webpack,还是 TypeScript 编译器有在编译过程中将绝对路径转换为相对路径的功能?
尝试过这种 tsconfig.json,但没有成功:
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"lib": ["es2017"],
"removeComments": true,
"sourceMap": true,
"baseUrl": "./",
"paths": {
"*": ["./*"]
},
"rootDir": "server",
"outDir": "generated",
"typeRoots": ["node_modules/@types"]
},
"include": [
"server/**/*.ts"
],
"exclude": [
"node_modules"
]
}
附注:使用 Node.js 并通过 ts-node 运行我的应用。