【发布时间】:2022-01-20 02:46:43
【问题描述】:
我尝试使用 TypeScript 从头开始创建 NodeJS,并且文件结构如下:
my-project
|
|--node_modules
|--src
| |--index.ts
| |--test.ts
|
|--package.json
|--tsconfig.json
|--...
然后在tsconfig.json 中,我添加了baseUrl 和paths 以允许绝对导入
{
"compilerOptions": {
"target": "ES5",
"lib": ["es5", "es6", "ESNext"],
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "CommonJS",
"moduleResolution": "Node",
"baseUrl": ".",
"paths": {
"@src/*": ["src/*"],
"@root/*": ["*"]
},
"resolveJsonModule": true,
"sourceMap": true,
"outDir": "./build",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"exclude": ["node_modules", "build"]
}
我创建了这样的简单代码,VSCode 仍然检测到test.ts 并通过严格模式的 linter 检查。
// src/test.ts
export const a = 1;
// src/index.ts
import { a } from '@src/test'
console.log(a);
之后,我运行npx nodemon src/index.ts,终端显示错误:
Error: Cannot find module '@src/test'
Require stack:
- D:\self-project\typeorm-test\src\index.ts
我是否配置路径错误?有谁知道如何解决这个问题?
【问题讨论】:
标签: node.js typescript tsconfig