nest-modules / mailer

📨 A mailer module for Nest framework (node.js)
https://nest-modules.github.io/mailer/
MIT License
846 stars 177 forks source link

TypeError: Converting circular structure to JSON #3

Closed krivochenko closed 6 years ago

krivochenko commented 6 years ago

Thanks for your work, but I encountered issue:

TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
    at ModuleTokenFactory.getDynamicMetadataToken (C:\www\project\node_modules\@nestjs\core\injector\module-token-factory.js:16:45)
    at ModuleTokenFactory.create (C:\www\project\node_modules\@nestjs\core\injector\module-token-factory.js:10:27)
    at NestContainer.addModule (C:\www\project\node_modules\@nestjs\core\injector\container.js:41:47)
    at DependenciesScanner.storeModule (C:\www\project\node_modules\@nestjs\core\scanner.js:31:24)
    at DependenciesScanner.scanForModules (C:\www\project\node_modules\@nestjs\core\scanner.js:21:14)
    at importedModules.map.innerModule (C:\www\project\node_modules\@nestjs\core\scanner.js:24:18)
    at Array.map (<anonymous>)
    at DependenciesScanner.scanForModules (C:\www\project\node_modules\@nestjs\core\scanner.js:23:25)
    at DependenciesScanner.scan (C:\www\project\node_modules\@nestjs\core\scanner.js:16:14)

Steps for reproducing:

  1. git clone https://github.com/nestjs/typescript-starter.git project
  2. cd project
  3. npm i
  4. npm install --save @nest-modules/mailer
  5. npm install --save nodemailer-mandrill-transport
  6. app.module.ts:
    
    import { Module } from '@nestjs/common';
    import { AppController } from './app.controller';
    import { MailerModule } from '@nest-modules/mailer';

@Module({ imports: [ MailerModule.forRoot(), ], controllers: [AppController], components: [], }) export class AppModule {}

7. mailerconfig.ts:

import * as mandrillTransport from 'nodemailer-mandrill-transport';

export = { transport: mandrillTransport({ auth: { api_key: 'key' } }), defaults: { from:'"nest-mailer" noreply@nestjs.com', }, templateDir: './src/common/email-templates' }


How I can resolve it? Thanks for help.
cdiaz commented 6 years ago

Hi @krivochenko, It's related with this issue and will be fixed soon, thanks for reporting

anyx commented 6 years ago

Looks like it wasn't fixed:

const mailerTransport = nodemailer.createTransport({
    sendmail: true,
    newline: 'unix',
    path: '/usr/sbin/sendmail',
});

@Module({
    imports: [
        MailerModule.forRoot({
            transport: mailerTransport,
            defaults: {
                from: config.get('mailer.from'),
            },
            templateDir: './templates/mailer',
        }),
...
$ node index.js
[Nest] 1828   - 2018-9-6 16:20:59   [Application] Converting circular structure to JSON
TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
    at ModuleTokenFactory.getDynamicMetadataToken (/srv/project/backend/node_modules/@nestjs/core/injector/module-token-factory.js:17:45)
    at ModuleTokenFactory.create (/srv/project/backend/node_modules/@nestjs/core/injector/module-token-factory.js:11:27)
    at ModuleCompiler.compile (/srv/project/backend/node_modules/@nestjs/core/injector/compiler.js:19:47)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:118:7)
    at Function.Module.runMain (module.js:692:11)
    at startup (bootstrap_node.js:194:16)
    at bootstrap_node.js:666:3
 1: node::Abort() [node]
 2: 0x8cea99 [node]
 3: v8::internal::FunctionCallbackArguments::Call(void (*)(v8::FunctionCallbackInfo<v8::Value> const&)) [node]
 4: 0xb1444c [node]
 5: v8::internal::Builtin_HandleApiCall(int, v8::internal::Object**, v8::internal::Isolate*) [node]
 6: 0x154bbd1842fd
Aborted
error Command failed with exit code 134.

nest version: 5.2.0

RafaelTCostella commented 6 years ago

Looks like it wasn't fixed:

const mailerTransport = nodemailer.createTransport({
    sendmail: true,
    newline: 'unix',
    path: '/usr/sbin/sendmail',
});

@Module({
    imports: [
        MailerModule.forRoot({
            transport: mailerTransport,
            defaults: {
                from: config.get('mailer.from'),
            },
            templateDir: './templates/mailer',
        }),
...
$ node index.js
[Nest] 1828   - 2018-9-6 16:20:59   [Application] Converting circular structure to JSON
TypeError: Converting circular structure to JSON
    at JSON.stringify (<anonymous>)
    at ModuleTokenFactory.getDynamicMetadataToken (/srv/project/backend/node_modules/@nestjs/core/injector/module-token-factory.js:17:45)
    at ModuleTokenFactory.create (/srv/project/backend/node_modules/@nestjs/core/injector/module-token-factory.js:11:27)
    at ModuleCompiler.compile (/srv/project/backend/node_modules/@nestjs/core/injector/compiler.js:19:47)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:118:7)
    at Function.Module.runMain (module.js:692:11)
    at startup (bootstrap_node.js:194:16)
    at bootstrap_node.js:666:3
 1: node::Abort() [node]
 2: 0x8cea99 [node]
 3: v8::internal::FunctionCallbackArguments::Call(void (*)(v8::FunctionCallbackInfo<v8::Value> const&)) [node]
 4: 0xb1444c [node]
 5: v8::internal::Builtin_HandleApiCall(int, v8::internal::Object**, v8::internal::Isolate*) [node]
 6: 0x154bbd1842fd
Aborted
error Command failed with exit code 134.

nest version: 5.2.0

Hello @anyx! You're making the same mistake that I!

Instead of:

const mailerTransport = nodemailer.createTransport({
    sendmail: true,
    newline: 'unix',
    path: '/usr/sbin/sendmail',
});

@Module({
    imports: [
        MailerModule.forRoot({
            transport: mailerTransport,
            defaults: {
                from: config.get('mailer.from'),
            },
            templateDir: './templates/mailer',
        }),
...

You should do:

const mailerTransport = {
    sendmail: true,
    newline: 'unix',
    path: '/usr/sbin/sendmail',
};

@Module({
    imports: [
        MailerModule.forRoot({
            transport: mailerTransport,
            defaults: {
                from: config.get('mailer.from'),
            },
            templateDir: './templates/mailer',
        }),
...

The reason is that because in this line: https://github.com/nest-modules/mailer/blob/4de893c48fe776398828ea994ca117c5ecb03c57/lib/mailer.provider.ts, mailerProvider already calls createTransport.

@cdiaz I think that we can improve docs with examples for different plugins, if you agree I would like to submit a new PR to that!

cdiaz commented 6 years ago

Hello @RafaelTCostella , your PR will be welcome, feel free to contribute. Thanks

RafaelTCostella commented 6 years ago

https://github.com/nest-modules/mailer/pull/8

Done!

cdiaz commented 6 years ago

Solved in v 0.4.1