Search code examples
node.jshandlebars.jsnext.jsnodemailervercel

Nodemailer + Handlebars + NextJS - The correct path


I'm experiencing problems to find the right path settings in production for my nodemailer/handlebars setup. The transporter is connected to an API request in order to send an automatic email.

In development I'm leaving the configuration as is, so basically I have this situation:

module.exports.ViewOption = (transport, hbs) => {
  transport.use('compile', hbs({
    viewEngine: {
      extname: '.hbs',
      partialsDir: 'static/js/mail/views/email',
      layoutsDir: 'static/js/mail/views',
      defaultLayout: 'contacts'
    },
    viewPath: 'static/js/mail/views',
    extName: '.hbs'
  }));
}

No problems here, everything works fine.

On Now instead, I'm receiving this exception:

Screenshot 2019-11-06 at 10.47.57.png

The request returns an ok response as you can see here:

Screenshot 2019-11-06 at 10.48.10.png

So I concluded that the problem is related to the wrong path set on transporter config. I can't live it like on dev because it redirects on /var/tasks. The problem is that I've tried different ways to locate the file path correctly, without success:

  • Using __dirname +'<path>'
  • Placing it in plain such as /<path>...
  • Setting the one inside _next production dir: /_next/static/....

This is my current filesystem:

Screenshot 2019-11-06 at 10.48.39.png

Screenshot 2019-11-06 at 10.49.25.png

As you can see, I've the config file both inside on /static/js/mail/config than /_next/static/js/mail/config. None of both worked, the call responses every time with the same error, independently by its path.

This is my current now.json if it could be helpful:

{
  "public": false,
  "name": "LC",
  "version": 2,
  "routes": [{
    "src": "/resume",
    "dest": "https://lucacattide.dev/static/html/resume.html"
  }, {
    "src": "/robots.txt",
    "dest": "https://lucacattide.dev/static/robots.txt"
  }, {
    "src": "/sitemap.xml",
    "dest": "https://lucacattide.dev/static/sitemap.xml"
  }, {
    "src": "^/service-worker.js$",
    "dest": "/_next/static/service-worker.js",
    "headers": {
      "cache-control": "public, max-age=43200, immutable",
      "Service-Worker-Allowed": "/"
    }
  }],
  "env": {
    "RECAPTCHA_SECRET_KEY": "@recaptcha",
    "SMTP_SERVICE_HOST": "@smtp_host",
    "SMTP_SERVICE_PORT": "@smtp_port",
    "SMTP_SERVICE_SECURE": "@smtp_secure",
    "SMTP_USER_NAME": "@smtp_name",
    "SMTP_USER_PASSWORD": "@smtp_password",
    "SMTP_LOG_NAME": "@smtp_log"
  },
  "github": {
    "enabled": true,
    "autoAlias": false
  }
}

Anyone experienced this before or could help me in some way?

Thanks in advance


Solution

  • As I stated on GitHub, at last I found the cause of __dirname misconfiguration, related to the Webpack configuration. As suggested on StackOverflow, by deactivating it on Webpack, as follows:

    {
      node: {
        __dirname: false
      }
    }
    

    Only the __dirname from Node configuration is being used on client-side.

    But, unfortunately, this doesn't solve my situation, because NextJS uses more than one Webpack config - both for server-side than client-side - so in my case, if I try to modify the client-side one, the returned path points to NextJS /pages/api folder.

    Due to the fact that by the serverless environment of my current Hosting - Zeit.co - is impossible to my to set up a proper express custom server as I would, I finally opted for a third-party service - EmailJS.