一. 前言
经过前两章我们已经逐步的完成了一个极简Nest服务器的搭建,而且是根据参考着Nest的源码实现的,所以我们在写自己代码的时候无法避免需要打开Nest的源码来进行参考
现在如果我想看人家Nest的源码,我只能手动的去node_modules找到@nestjs,但是我感觉太麻烦了,有没有一种好的方式呢?
举个例子
比如当我使用ctrl+鼠标左键想查看Controller这个装饰器时,可以打开正确的文件
例如我目前用的是本地自己开发的@nestjs如何确保它不会识别错误而跳转到node_modules下的@nestjs
import { Controller } from './@nestjs/common'
import { Get } from './@nestjs/common'
@Controller()
export class AppController {
@Get('hello')
hello(): string {
return 'hello'
}
}
二. 配置ts.config.json
增加paths配置: { "@nestjs/*": ["src/@nestjs/*"] }
{
"compilerOptions": {
"experimentalDecorators": true, // 启用装饰器功能
"target": "ES2021",
"module": "CommonJS",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true, // 为运行时提供额外的类型信息(可选)
"allowSyntheticDefaultImports": true,
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./", // 路径别名
"paths": {
"@nestjs/*": ["src/@nestjs/*"]
},
"incremental": true,
"skipLibCheck": true,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false,
"esModuleInterop": true
}
}
三. 修改启动命令
在使用ts-node来启动服务时,需要先安装tsconfig-paths这个模块用于解析路径别名,以确保在 TypeScript 文件中使用路径别名时能够正确解析
// package.json
"scripts": {
"start": "ts-node -r tsconfig-paths/register src/main.ts",
},