Search code examples
nestjshealth-check

MIssing TERMINUS_ERROR_LOGGER argument/dependency - NestJS HealthCheck controller


I'm trying to implement a simple HealtCheck controller in my NestJS api by following the offical example Healthchecks (Terminus): https://docs.nestjs.com/recipes/terminus#setting-up-a-healthcheck but I'm getting this error:

Nest can't resolve dependencies of the HealthCheckService (HealthCheckExecutor, ?). Please make sure that the argument TERMINUS_ERROR_LOGGER at index [1] is available in the HealthCheckModule context.

Since TERMINUS_ERROR_LOGGER seems like some kind of a enum, I'm not able to import it or add it as a provider inside my HealthModule and I haven't found any documentation/blog/post related to this.

Here's my HealthCHeck controller code:

import { Controller, Get } from '@nestjs/common';
import { HealthCheckService, HttpHealthIndicator, HealthCheck, TypeOrmHealthIndicator } from '@nestjs/terminus';

@Controller('health-check')
export class HealthCheckController {

    constructor(
        private readonly health: HealthCheckService,
        private db: TypeOrmHealthIndicator,
    ) { }

    @Get()
    @HealthCheck()
    readiness() {
        return this.health.check([
            async () => this.db.pingCheck('postgres', { timeout: 300 }),
        ]);
    }
}

I just want to create a HealtCheck controller to check if the api is connected to my db and implement future health checks.


Solution

  • I recently had the same error with one difference - it was happening in a Nest TestingModule. I'm hoping my solution will help other folks who hit this same issue.

    This is using NestJS 10, and I came upon this issue when upgrading a repo from 9 to 10.

    Here's what it looked like when the error was happening:

    const module: TestingModule = await Test.createTestingModule({
      controllers: [HealthController],
      providers: [
        MemoryHealthIndicator,
        HealthCheckService,
        HealthCheckExecutor,
      ],
    }).compile();
    

    First attempt to fix it was to import the TerminusModule into the test module:

    const module: TestingModule = await Test.createTestingModule({
      imports: [TerminusModule],
      controllers: [HealthController],
      providers: [
        MemoryHealthIndicator,
        HealthCheckService,
        HealthCheckExecutor,
      ],
    }).compile();
    

    But that did not change the error. Then I looked at the real module this was testing, and noticed that neither HealthCheckService nor HealthCheckExecutor were listed as providers there, so I removed those apparently unneeded providers from the array:

    const module: TestingModule = await Test.createTestingModule({
      imports: [TerminusModule],
      controllers: [HealthController],
      providers: [MemoryHealthIndicator],
    }).compile();
    

    That did it! So what I would suggest is taking a look at whatever module is using your HealthCheckController and seeing if there are providers that you do not appear to need. You will also need to make sure you're importing the TerminusModule.

    (When I experimented and put HealthCheckService and HealthCheckExecutor in the array of providers on the real module, the same error occurred.)