Tea Zulo
Tea Zulo

Reputation: 105

How do I import a global provider into another global provider in NestJS?

I want to import the config provider from the documentation into another provider.

I am using the same config structure that is sown in the documentation here.

So the config.module.ts looks like this:

import { Module, Global } from '@nestjs/common';
import { ConfigService } from './config.service';

@Global()
@Module({
  providers: [
    {
      provide: ConfigService,
      useValue: new ConfigService(
        `config/env/${process.env.NODE_ENV || 'development'}.env`,
      ),
    },
  ],
  exports: [ConfigService],
})
export class ConfigModule {}

And the other provider should look something like this correct?

token.module.ts

import { Module, Global } from '@nestjs/common';
import { TokenService} from './token.service';
import { ConfigService } from './config.service';

@Global()
@Module({
  import: [ConfigService]
  providers: [TokenService],
  exports: [TokenService],
})
export class TokenModule {}

While the TokenService should look something like this

token.service.ts

import { ConfigService } from '../../config/config.service';

export class TokenService {
  constructor(private readonly configService: ConfigService) {}

  test(): string {
    return this.configService.get('TestVal');
  }
}

But when I import TokenModule in AppModule I get this error The "path" argument must be one of type string, Buffer, or URL. Received type undefined

Upvotes: 0

Views: 1399

Answers (1)

Ruan Mendes
Ruan Mendes

Reputation: 92314

You forgot to mark your TokenService as @Injectable(). You need to tell NestJS about arguments that need to be provided to other services.

Upvotes: 1

Related Questions