【发布时间】:2022-01-10 02:49:58
【问题描述】:
我有这个项目结构:
如何配置tsconfig.json 以使用paths,例如@app/、@server/。
我试试这个:
{
"compilerOptions": {
"module": "CommonJS",
"target": "es5",
"lib": [
"esnext",
"dom"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": false,
"baseUrl": "..",
"paths": {
"@app/*": [
"app/*"
],
"@server/*": [
"server/*"
]
}
},
"include": [
"."
]
}
相同的配置适用于webpack 和ts-loader,但是当我运行npx ts-node server/index.ts 时出现错误:
npx ts-node server/index.ts
Error: Cannot find module '@server/a'
server/index.ts :
import a from '@server/a'
console.log('This is index.ts')
a()
【问题讨论】:
-
这能回答你的问题吗? How to use paths in tsconfig.json?
-
@about14sheep,不,我很喜欢这个解决方案。如果
tsconfig.json在项目根文件夹中,此解决方案stackoverflow.com/a/56162649/4482323 有效,但如果tsconfig.json在server/文件夹中则无效。
标签: node.js typescript ts-node