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.