'How to use INestApplicationContext with Module Providers in Typescript NestJS?

I would like to add TestAppGateway to the providers of TestAppModule. However, the following error occured. I found that it is due to INestApplicationContext in the constructor of TestAppGateway because the error disappeared when I tried to remove it. But if I removed it, other functions will not work. May I know how to solve it by keeping INestApplicationContext in the constructor? Thanks.

[Nest] 16336  - 13/04/2022, 6:15:44 pm   ERROR [ExceptionHandler] Nest can't resolve dependencies of the TestAppGateway (?). Please make sure that the argument Object at index [0] is available in the TestAppModule context.

Potential solutions:
- If Object is a provider, is it part of the current TestAppModule?
- If Object is exported from a separate @Module, is that module imported within TestAppModule?
  @Module({
    imports: [ /* the Module containing Object */ ]
  })

Error: Nest can't resolve dependencies of the TestAppGateway (?). Please make sure that the argument Object at index [0] is available in the TestAppModule context.

Potential solutions:
- If Object is a provider, is it part of the current TestAppModule?
- If Object is exported from a separate @Module, is that module imported within TestAppModule?
  @Module({
    imports: [ /* the Module containing Object */ ]
  })

    at Injector.lookupComponentInParentModules (D:\www\tests\workspace\test-service\node_modules\@nestjs\core\injector\injector.js:231:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at Injector.resolveComponentInstance (D:\www\tests\workspace\test-service\node_modules\@nestjs\core\injector\injector.js:184:33)
    at resolveParam (D:\www\tests\workspace\test-service\node_modules\@nestjs\core\injector\injector.js:106:38)
    at async Promise.all (index 0)
    at Injector.resolveConstructorParams (D:\www\tests\workspace\test-service\node_modules\@nestjs\core\injector\injector.js:121:27)
    at Injector.loadInstance (D:\www\tests\workspace\test-service\node_modules\@nestjs\core\injector\injector.js:52:9)
    at Injector.loadProvider (D:\www\tests\workspace\test-service\node_modules\@nestjs\core\injector\injector.js:74:9)
    at async Promise.all (index 4)
    at InstanceLoader.createInstancesOfProviders (D:\www\tests\workspace\test-service\node_modules\@nestjs\core\injector\instance-loader.js:44:9)

Below is my code:

test-app.module.ts

import { INestApplicationContext, Module } from '@nestjs/common';
import { TestAppController } from './test-app.controller';
import { TestAppService } from './test-app.service';
import { TestAppGateway } from './test-app.gateway';

@Module({
  imports: [],
  controllers: [TestAppController],
  providers: [TestAppService, TestAppGateway],
})
export class TestAppModule {}

test-app.gateway.ts

import { INestApplicationContext, Injectable } from '@nestjs/common';
import { TestAppService } from './test-app.service';

constructor(app: INestApplicationContext) {
   this.app = app;
   this.testAppService = app.get(TestAppService);
}

index.ts

import { TestAppGateway } from './test-app.gateway';

export default class TestApp {
  private testAppGateway: TestAppGateway;

  public async startApp() {
    const app = await NestFactory.create(TestAppModule, {
      bodyParser: true,
    });

    this.testAppGateway = new TestAppGateway(app);
    this.testAppGateway.run();
  }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source