get-convex / convex-js

TypeScript/JavaScript client library for Convex
https://docs.convex.dev
Apache License 2.0
106 stars 12 forks source link

Trouble using nodemailer package with "use node" flag #7

Closed reanzi closed 4 months ago

reanzi commented 8 months ago

I'm trying to send an email (a verification code), which should only happen in the server in convex I've have used "use node" flag at the top of the file. and imported the package, in the convex env I added the envs as needed Below is function `export const sendMail = async ({ name, to, subject, body }: { to: string, name: string, subject: string, body: string, }) => { const { SMTP_EMAIL, SMTP_PASSWORD } = process.env;

const transport = createTransport({
    service: "gmail",
    auth: {
        user: SMTP_EMAIL,
        pass: SMTP_PASSWORD
    }
});

try {
    const testResult = await transport.verify()
    console.log("TEST: ", testResult);
} catch (error) {
    console.log("Mail Error: ", error)
}

try {
    const sendResult = await transport.sendMail({
        from: SMTP_EMAIL,
        to,
        subject,
        html: body,
    });
    console.log(sendResult);
} catch (error) {
    console.log(error);
    return
}

}`

but npx convex dev fails with errors:

node_modules/.pnpm/nodemailer@6.9.11/node_modules/nodemailer/lib/dkim/message-parser.js:3:26:
      3 │ const Transform = require('stream').Transform;
        ╵                           ~~~~~~~~

  The package "stream" wasn't found on the file system but is built  
  into node. Are you trying to bundle for node? You can use
  "platform: 'node'" to do that, which will remove this error.       

X [ERROR] Could not resolve "crypto"

    node_modules/.pnpm/nodemailer@6.9.11/node_modules/nodemailer/lib/dkim/sign.js:5:23:
      5 │ const crypto = require('crypto');
        ╵                        ~~~~~~~~

  The package "crypto" wasn't found on the file system but is built  
  into node. Are you trying to bundle for node? You can use
  "platform: 'node'" to do that, which will remove this error.  

removing the function body even with the import inplace, convex compile just fine, what am I missing or doing wrong?

NOTE: Other packages works just fine like svix and bcrypt-ts

thomasballinger commented 4 months ago

It looks like you are importing this file from one which does not have "use node", these are bundler errors saying these Node.js builtins can't be bundled.