good-request
good-request

Reputation: 25

Nest can't resolve dependencies of the MailerService (?)

I'm trying to configure this module.

The full error message reads as follows:

Nest can't resolve dependencies of the MailerService (?). Please make sure that the argument MAILER_OPTIONS at index [0] is available in the AppModule context.

I keep getting this error out of nowhere, what MAILER_OPTIONS? Where are those options? Nothing is said in the documentation in this respect. Absolutely no idea what's going on.

Here is my AppModule:

@Module({
  imports: [
    MailerModule.forRoot({
      defaults: {
        from: '"No Reply" <[email protected]>',
      },
      template: {
        dir: path.join(process.env.PWD, 'templates/pages'),
        adapter: new HandlebarsAdapter(),
        options: {
          strict: true,
        },
      },
      options: {
        partials: {
          dir: path.join(process.env.PWD, 'templates/partials'),
          options: {
            strict: true,
          },
        },
      },
    }),
  ],
  controllers: [AppController],
  providers: [
    AppService,
    MailerService,
  ],
})
export class AppModule {}

Any ideas?

Here is the service I'm using:

@Injectable()
export class AppService {
  constructor(
    private readonly mailerService: MailerService,
  ) {}

  public async sendEmail(): Promise<any> {
    const mailDetail: ISendMailOptions = {
      to: '[email protected]',
      from: '[email protected]',
      subject: 'testing Nest MailerModule',
      text: 'test test?!',
      html: '<b>Yahooooo</b>',
    };

    return this.mailerService.sendMail(mailDetail);
  }
}

Upvotes: 1

Views: 3872

Answers (3)

EagleDLuis
EagleDLuis

Reputation: 1

I solve this problem just removing MailerService because it is part of the library in node modules and it work for me

Upvotes: 0

v.ng
v.ng

Reputation: 794

If you encountered the same error on unit test, maybe you have defined MailerModule in AppModule instead of a separate module, it will make unit test fails because you normally don't import the whole AppModule in unit test. Make sure you defined all the MailerModule on a separate module, like MailModule, and import this module in the AppModule.

@Module({
  imports: [
    EmailModule
  ],
  controllers: [AppController],
  providers: [
    AppService,
  ],
})
export class AppModule {}

and

imports...
@Module({
  imports: [
    ConfigModule,
    MailerModule.forRoot({
      defaults: {
        from: '"No Reply" <[email protected]>',
      },
      template: {
        dir: path.join(process.env.PWD, 'templates/pages'),
        adapter: new HandlebarsAdapter(),
        options: {
          strict: true,
        },
      },
      options: {
        partials: {
          dir: path.join(process.env.PWD, 'templates/partials'),
          options: {
            strict: true,
          },
        },
      },
    }),
  ],
  exports: [NestMailerModule],
})
export class EmailModule {}

Then also import the module to your RootTestModule

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

Upvotes: 0

Kim Kern
Kim Kern

Reputation: 60457

You have to remove the imported MailerService from the providers array of your AppModule. Only declare providers that are part of the module itself; you would never declare an imported provider (service).

  providers: [
    AppService,
  ],

Upvotes: 5

Related Questions