Cannot find module "@sendgrid/mail"
Asked Answered
N

3

5

I'm using the Sendgrid mail package (https://www.npmjs.com/package/@sendgrid/mail) to send a test email using the Twilio Serveless functions. I have configured the module, specifying the correct version and module in the configure dashboard here. https://www.twilio.com/console/functions/configure but when I deploy my function and run it using the twilio cli, i get the error message,

"message":"Cannot find module '@sendgrid/mail'"

I find this weird since deploying the function manually under the Manage tab, https://www.twilio.com/console/functions/manage tab works like a gem. I'm I missing something?

Or does the serverless API doesn't currently support this? (the same package configurations work when I manually deploying the function)

Nealy answered 10/11, 2019 at 6:29 Comment(0)
D
5

the Twilio GUI Console based Functions are separate and distinct from the API based functions. You can find more detail here.

Beta limitations, known issues and limits

You can add the npm module(s) using npm install , as detailed here.

Twilio Serverless Toolkit - Deploy a Project

"Any dependency that is listed in the dependencies field in your package.json will automatically be installed in your deployment."

If you use the Visual Studio Code approach, you can do the same.

Dynamoelectric answered 10/11, 2019 at 13:33 Comment(0)
V
2

just use:

yarn add @sendgrid/mail
Villalpando answered 4/12, 2022 at 22:1 Comment(0)
S
0
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: '[email protected]',
  from: '[email protected]',
  subject: 'Sending with Twilio SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
  html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
//ES6
sgMail
  .send(msg)
  .then(() => {}, console.error);
//ES8
(async () => {
  try {
    await sgMail.send(msg);
  } catch (err) {
    console.error(err.toString());
  }
})();
Saied answered 19/2, 2020 at 12:34 Comment(1)
While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.Mcsweeney

© 2022 - 2024 — McMap. All rights reserved.