简体   繁体   中英

Cannot find module Interceptor NestJS

I followed the NestJS's docs for setting up a interceptor, but i'm facing following issue:

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)

src/middleware/request.interceptor.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`)),
        );
    }
}

src/product/controller/product.controller.ts

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

    constructor(private configService: ConfigService) {}

    @Get()
    findAll(): String {
        return "hello";
    }
}

src/product/product.module

@Module({
    imports: [ConfigModule],
    controllers: [ProductController]
})
export class ProductModule {}
@Module({
  imports: [ProductModule],
  controllers: [AppController],
  providers: [AppService, Logger],
})
export class AppModule {}

I strictly followed the docs, i already tried to rebuild and delete dist folder. Am i missing something?

Just found the reason.

The problem is the damn VsCode auto import path.

In product.controller the auto generated import was import { LoggingInterceptor } from "src/middleware/request.interceptor"; but it should be import { LoggingInterceptor } from "../../middleware/request.interceptor";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM