0

This is the javascript file:

import nodemailer from 'nodemailer';
function SendEmail() {
    let message = {
    from: '"foo@example.com" <foo@example.com>',
    to: "example@example.com",
    subject: "Hello",
    text:"Hello world",
    };
    const transporter = nodemailer.createTransport({
        host: 'smtp.gmail.com',
        port: 587,
        secure: false,
        requireTLS: true,
        auth: {
            user: 'email',
            pass: 'password'
        },
        logger: true
    });
    transporter.sendMail(message);
}

This is ho the HTML looks like:

<!DOCTYPE html>
<html>
<head>
    <script type="module" src="sendEmail.js"></script>
</head>
<body>
    <input onclick="SendEmail()" value="Submit">
</body>
</html>

When I open the HTML document I get the error: Error resolving module specifier “nodemailer”. Relative module specifiers must start with “./”, “../” or “/”.

And when I click on the input I get the error: Uncaught ReferenceError: SendEmail is not defined

But when I instantly call the SendEmail function and run the script using the command : node sendEmail.js it works. What's the problem?

Incognito
  • 1
  • 1
  • Are you trying to use Node.js modules in a browser? I wouldn't expect that to work at all unless the module vendor specifically indicates otherwise. But either way, to import a module in a browser you need to specify the path, not just the name. – David Feb 22 '22 at 18:18
  • Ok I'll try it to see if it works – Incognito Feb 22 '22 at 18:24
  • I get the error : Loading module from “http://localhost:8000/node_modules/@types/nodemailer/index” was blocked because of a disallowed MIME type (“text/html”). – Incognito Feb 22 '22 at 19:01
  • Well, `text/html` implies that what's being requested wasn't JavaScript code. When you request that exact URL in the browser, what is the response? As an aside, it's worth reiterating that modules designed for Node.js are unlikely to work at all in a browser unless the vendor specifically indicates otherwise. (And if they do, presumably they provide documentation/examples?) – David Feb 22 '22 at 19:07

0 Answers0