我曾经遇到过解决打印稿中非相关模块的问题。然后尝试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'
3条答案
按热度按时间5tmbdcev1#
最后,我添加
paths
选项来解决它。tsconfig.json
在app.ts中:
to94eoyn2#
您需要像这样将tsconfig-paths和module-alias添加到 package.json 中:
在 app.ts 中使用:用途:
您还需要在根目录中创建 * tslog-path.js* 文件,该文件应该如下所示:
同时更改 * tsconfig.json *:
ac1kyiln3#
你需要安装
tsconfig-paths
并在tsconfig.json
文件中添加以下内容:参考:https://github.com/TypeStrong/ts-node#paths-and-baseurl