0

My problem was: I was using require in a js script used in the browser (to download nodemailer). Following the advice of this SO post, I downloaded browserify. It allowed me to use require in a js file used by the browser.

Webpack was another alternative listed. It is more common than browserify & has more features (like intelligently arranging files that import & export (from one another)).

However, it looks like webpack doesn't work with nodemailer. Here is my error code when I try to make my bundle.js file:

enter image description here

My question is:

  • Is my summation of my problems, and my steps to solve them, reasonable?
  • If so, what are some common solutions people're using? For instance: I like webpack & would like to use libraries in `js` scripts. Are people using both?
tonitone120
  • 1,920
  • 3
  • 8
  • 25

1 Answers1

1

Webpack has no problems handling require, that is its raison d'être!

You can't polyfill Nodemailer. It depends on APIs that Node.js supplies but which are not available in web browsers.

If you want to send email from a web browser then the usual approach is to make an HTTP request to a web service which sends the email.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I was able to use nodemailer with `Node.js` & express. Trying to use nodemailer with just browserify and that's not working (the error I get when I *run the webpage* is: `net.isIP is not a function`). Is that normal? Is that what you meant by `you can't polyfill nodemailer`? I'm learning React. I wonder if that'll make it easier to use nodemailer. – tonitone120 Oct 17 '20 at 11:20
  • Here's someone with a similar line of questioning: https://stackoverflow.com/questions/55163689/react-nodemailer-net-isip-is-not-a-function. I'd like to know why it works in express but not via browserify (or react). I thought the whole point of browserify was to be able to use libraries in browser scripts. My only guess is that it requires connecting to something online and perhaps express helps with this. Thanks for your response btw. – tonitone120 Oct 17 '20 at 11:30
  • 1
    See the second paragraph of the answer. To send an email you need (for the sake of argument) to be able to open a raw socket to communicate SMTP over. Node.js provides that. A web browser doesn’t. React runs in the browser, it doesn’t make it possible (let alone easier) to use Nodemailer there. – Quentin Oct 17 '20 at 13:57
  • Okay. The tutorial I followed used express & 'express-handlbars' (with extension `.handlebar`) and express rendered some tags on a local port. Can you tell if this project was running from `Node.js` or running from a web browser? In the end I discovered EmailJS which works just in the front-end. That does for now. – tonitone120 Oct 17 '20 at 14:27
  • The tutorial you followed would have involved setting up a webserver running via Node.js. EmailJS uses a web service … it just happens to be a third-party web service who you have to give the keys to your SMTP server to. I don't trust them. – Quentin Oct 17 '20 at 14:29