首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:使用sendgrid时找不到模块“sendgrid”

错误:使用sendgrid时找不到模块“sendgrid”
EN

Stack Overflow用户
提问于 2020-11-09 08:17:18
回答 1查看 1.3K关注 0票数 0

我一直收到一个“错误:无法在我的控制台中找到模块'sendgrid‘时,试图执行我的代码。我以前从未使用过sendgrid,我做错什么了吗?

代码语言:javascript
复制
 SENDGRID =
  "<key>";

const sgMail = require("sendgrid").SendGrid(conf);
sgMail.setApiKey(process.env.SENDGRID);

const msg = {
  to: "eden.cmo@gmail.com", // Change to your recipient
  from: "test.test@gmail.com", // Change to your verified sender
  subject: "Sending with 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>",
};

sgMail
  .send(msg)
  .then(() => {
    console.log("Email sent");
  })
  .catch((error) => {
    console.error(error);
  });

Error: Cannot find module 'sendgrid'
Require stack:
- /Users/tategraham/Downloads/send grid/sendGrid.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:900:15)
    at Function.Module._load (node:internal/modules/cjs/loader:745:27)
    at Module.require (node:internal/modules/cjs/loader:972:19)
    at require (node:internal/modules/cjs/helpers:88:18)
    at Object.<anonymous> (/Users/tategraham/Downloads/send grid/sendGrid.js:4:16)
    at Module._compile (node:internal/modules/cjs/loader:1083:30)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1112:10)
    at Module.load (node:internal/modules/cjs/loader:948:32)
    at Function.Module._load (node:internal/modules/cjs/loader:789:14)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:72:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/Users/tategraham/Downloads/send grid/sendGrid.js' ]
}
tategraham@MacBook-Pro send grid % 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-09 08:28:47

代码语言:javascript
复制
const sgMail = require('@sendgrid/mail')
sgMail.setApiKey(process.env.SENDGRID_API_KEY)

这里有一个指向实际文档的链接: https://sendgrid.com/docs/for-developers/sending-email/quickstart-nodejs/#complete-code-block

使用环境变量更新

首先创建类似于.env的sendgrid.env文件。然后你可以使用require('dotenv').config(sendgrid.env)或者可能,只使用require('dotenv').config(sendgrid)

然后,您可以使用在该env文件中定义的SENDGRID_API_KEY,至少根据我对您问题的理解可以这样做。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64747951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档