我正在尝试设置一些 typescript 别名来优化我的导入,而无需在 node.js 项目上使用 babel 或 webpack。
我目前的tsconfig.json
:
{
"compilerOptions": {
"baseUrl": ".",
"target": "es6",
"module": "CommonJS",
"moduleResolution": "node",
"allowJs": false,
"esModuleInterop": true,
"noImplicitAny": true,
"resolveJsonModule": true,
"outDir": "./build",
"strict": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"typeRoots": [
"node_modules/@types",
"src/@types"
],
"paths": {
"@common": ["src/common"],
}
},
"include": ["./src/**/*", "./db/**/*"],
"exclude": ["./**/__tests__/*", "./**/__functionaltests__/*", "types"]
}
公共包含:
util1.ts
util2.ts
index.ts
其中 index.ts 用作桶。
我还添加了 import 'module-alias/register';
在 package.json
上,我添加了:
"_moduleAliases": {
"@common": "build/src/common"
}
但是当我运行编译后的 JS 代码时仍然出现错误:
Error: Cannot find module '@common'
任何想法可能是什么问题?谢谢!
打字稿版本:4.2.4