admin管理员组

文章数量:1024026

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);
  }
}

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);
  }
}
Share Improve this question edited May 17, 2020 at 12:58 Kim Kern 60.7k20 gold badges219 silver badges214 bronze badges asked May 17, 2020 at 12:08 good-requestgood-request 252 silver badges7 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

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,
  ],

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],
    }).pile();

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

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);
  }
}

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);
  }
}
Share Improve this question edited May 17, 2020 at 12:58 Kim Kern 60.7k20 gold badges219 silver badges214 bronze badges asked May 17, 2020 at 12:08 good-requestgood-request 252 silver badges7 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

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,
  ],

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],
    }).pile();

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

本文标签: javascriptNest can39t resolve dependencies of the MailerService ()Stack Overflow