【问题标题】:Cannot find module Interceptor NestJS找不到模块拦截器 NestJS
【发布时间】:2020-06-23 23:50:33
【问题描述】:

我按照NestJS's docs 设置拦截器,但遇到以下问题:

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 {}

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

【问题讨论】:

    标签: javascript node.js typescript nestjs


    【解决方案1】:

    刚刚找到原因。

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

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

    【讨论】:

      猜你喜欢
      • 2020-12-16
      • 2021-01-18
      • 2021-08-12
      • 2020-01-03
      • 2020-12-12
      • 1970-01-01
      • 2020-06-22
      • 2020-10-14
      • 1970-01-01
      相关资源
      最近更新 更多