简体   繁体   中英

Cannot find module "@sendgrid/mail"

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)

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.

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: 'test@example.com',
  from: 'test@example.com',
  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());
  }
})();

just use:

yarn add @sendgrid/mail

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM