找不到模块拦截器 NestJS

Max*_*ane 2 javascript node.js typescript nestjs

我按照NestJS 的文档设置拦截器,但我面临以下问题:

Error: Cannot find module 'src/middleware/request.interceptor'
     at Function.Module._resolveFilename (module.js:548:15)
     at Function.Module._load (module.js:475:25)
     at Module.require (module.js:597:17)
     at require (internal/module.js:11:18)
     at Object.<anonymous> (/mypath/dist/product/controller/product.controller.js:15:31)
     at Module._compile (module.js:653:30)
     at Object.Module._extensions..js (module.js:664:10)
     at Module.load (module.js:566:32)
     at tryModuleLoad (module.js:506:12)
     at Function.Module._load (module.js:498:3)
Run Code Online (Sandbox Code Playgroud)

src/middleware/request.interceptor.ts

Error: Cannot find module 'src/middleware/request.interceptor'
     at Function.Module._resolveFilename (module.js:548:15)
     at Function.Module._load (module.js:475:25)
     at Module.require (module.js:597:17)
     at require (internal/module.js:11:18)
     at Object.<anonymous> (/mypath/dist/product/controller/product.controller.js:15:31)
     at Module._compile (module.js:653:30)
     at Object.Module._extensions..js (module.js:664:10)
     at Module.load (module.js:566:32)
     at tryModuleLoad (module.js:506:12)
     at Function.Module._load (module.js:498:3)
Run Code Online (Sandbox Code Playgroud)

src/产品/控制器/product.controller.ts

@Injectable()
export class LoggingInterceptor implements NestInterceptor {

    intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
        console.log('Before...');

        const now = Date.now();
        return next
            .handle()
            .pipe(
            tap(() => console.log(`After... ${Date.now() - now}ms`)),
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

src/产品/产品.模块

@Controller('product')
@UseInterceptors(LoggingInterceptor)
export class ProductController {

    constructor(private configService: ConfigService) {}

    @Get()
    findAll(): String {
        return "hello";
    }
}
Run Code Online (Sandbox Code Playgroud)
@Module({
    imports: [ConfigModule],
    controllers: [ProductController]
})
export class ProductModule {}
Run Code Online (Sandbox Code Playgroud)

我严格遵循文档,我已经尝试重建和删除dist文件夹。我错过了什么吗?

Max*_*ane 7

刚刚找到原因了

问题是该死的 VsCode 自动导入路径。

product.controller自动生成的导入 中import { LoggingInterceptor } from "src/middleware/request.interceptor";,但应该是import { LoggingInterceptor } from "../../middleware/request.interceptor";