我遇到了解决打字稿中非相对模块的问题。并尝试baseUrl
,paths
等等,但它不起作用....
tsconfig.json
{
"compilerOptions": {
"allowJs": true,
"baseUrl": ".",
"esModuleInterop": true,
"module": "commonjs",
"sourceMap": true,
"strict": true,
"target": "esnext",
"typeRoots": ["node_modules/@types"]
}
}
项目目录:
root
├─src
├── APIs
├── config
├── constants
├── middlewares
├── models
├── routes
├── services
- foo.ts
├── utils
└── app.ts
在app.ts中
import foo from 'src/services/foo'
并运行ts-node src/app.ts
.
但是会发生错误:
Cannot find module 'src/services/foo'