jun*_*lin 10 typescript ts-node
我遇到了解决打字稿中非相对模块的问题。并尝试等等,但它不起作用baseUrl......paths
tsconfig.json
\n\n{\n "compilerOptions": {\n "allowJs": true,\n "baseUrl": ".",\n "esModuleInterop": true,\n "module": "commonjs",\n "sourceMap": true,\n "strict": true,\n "target": "esnext",\n "typeRoots": ["node_modules/@types"]\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n项目目录:
\n\nroot\n \xe2\x94\x9c\xe2\x94\x80src\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 APIs\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 config\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 constants\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 middlewares\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 models\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 routes\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 services\n - foo.ts \n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 utils\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 app.ts\nRun Code Online (Sandbox Code Playgroud)\n\n在app.ts中
\n\nimport foo from \'src/services/foo\'\nRun Code Online (Sandbox Code Playgroud)\n\n并与 一起运行ts-node src/app.ts。
但出现错误:
\n\nCannot find module \'src/services/foo\'\nRun Code Online (Sandbox Code Playgroud)\n
最后,我添加paths选项来解决它。
tsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"esModuleInterop": true,
"module": "commonjs",
"moduleResolution": "node",
"removeComments": true,
"sourceMap": true,
"target": "esnext",
"strict": true,
"noUnusedLocals": true,
"paths": {
"@/*": ["src/*"]
},
"typeRoots": ["./src/@types", "./node_modules/@types"]
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/node_modules/*", "**/*.spec.ts"]
}
Run Code Online (Sandbox Code Playgroud)
在app.ts中:
import foo from '@/services/foo'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6654 次 |
| 最近记录: |