0

I am trying to have an API endpoint pages/api/sendEmail that uses nodemailer to send an email, but for some reason I get the error Can't resolve 'fs' when I try to use nodemailer with let nodemailer = require('nodemailer'). I have seen multiple posts about this always saying that this is due to the fact that I am trying to execute this in the frontend, but I am doing it in the API. I have also seen posts like this one and tried the fixes by modifying next.config.js, but after all the fixes this new error appeared:

Uncaught TypeError: util__WEBPACK_IMPORTED_MODULE_3__.TextEncoder is not a constructor

What I don't understand here is why can't I use the package in the API, since it is executed on the backend. I have seen multiple tutorials like this one that do the exact same thing.

nicase
  • 106
  • 1
  • 4
  • Can you share the code in your API route? Also, are you sure the error comes from that file? Are you not importing `nodemailer` (or any other Node.js-only package) somewhere else in client-side code? – juliomalves Jun 12 '22 at 17:18
  • @juliomalves At the end I managed to solve it by updating the webpack (currently I have version 6.15.16) and setting all the errors I found to `false` in the `next.config.js`, in a similar way as it is stated [here](https://stackoverflow.com/questions/64926174/module-not-found-cant-resolve-fs-in-next-js-application?noredirect=1&lq=1). I ended up with the following: `config.resolve.fallback = { fs: false, tls:false, dns:false, child_process:false }` – nicase Jun 14 '22 at 09:26

0 Answers0