Javascript 错误:找不到模块';sendgrid';使用sendgrid时

Javascript 错误:找不到模块';sendgrid';使用sendgrid时,javascript,node.js,firebase,sendgrid,Javascript,Node.js,Firebase,Sendgrid,我不断收到一个“错误:在试图执行代码时,在控制台中找不到模块“sendgrid”。我以前从未使用过sendgrid,我做错什么了吗 SENDGRID = "<key>"; const sgMail = require("sendgrid").SendGrid(conf); sgMail.setApiKey(process.env.SENDGRID); const msg = { to: "eden.cmo@gmail.c

我不断收到一个“错误:在试图执行代码时,在控制台中找不到模块“sendgrid”。我以前从未使用过sendgrid,我做错什么了吗

 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 % 
SENDGRID=
"";
const sgMail=require(“sendgrid”).sendgrid(conf);
setApiKey(process.env.SENDGRID);
常数msg={
致:“伊甸园。cmo@gmail.com“,//更改为您的收件人
来自:“测试。test@gmail.com“,//更改为您的已验证发件人
主题:“使用SendGrid发送很有趣”,
text:“在任何地方都可以轻松操作,即使使用Node.js”,
html:“并且在任何地方都可以轻松操作,即使使用Node.js”,
};
邮政总局
.send(msg)
.然后(()=>{
控制台日志(“已发送电子邮件”);
})
.catch((错误)=>{
控制台错误(error);
});
错误:找不到模块“sendgrid”
需要堆栈:
-/Users/tategraham/Downloads/send grid/sendGrid.js
位于Function.Module.\u解析文件名(节点:internal/modules/cjs/loader:900:15)
at功能模块加载(节点:内部/模块/cjs/加载程序:745:27)
at Module.require(节点:内部/模块/cjs/加载器:972:19)
需要时(节点:内部/模块/cjs/helpers:88:18)
反对。(/Users/tategraham/Downloads/send grid/sendGrid.js:4:16)
at模块编译(节点:内部/modules/cjs/loader:1083:30)
在Object.Module._extensions..js(节点:internal/modules/cjs/loader:1112:10)
at Module.load(节点:内部/模块/cjs/加载器:948:32)
at功能模块加载(节点:内部/模块/cjs/加载程序:789:14)
在Function.executeUserEntryPoint[作为runMain](节点:内部/modules/run\u main:72:12){
代码:“未找到模块”,
requireStack:['/Users/tategraham/Downloads/send grid/sendGrid.js']
}
tategraham@MacBook-专业发送网格%
这里有一个指向实际文档的链接:

使用环境变量的更新

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


然后,您可以使用该env文件中定义的SENDGRID\u API\u密钥,至少根据我对您问题的理解,可以这样做。

1。不要发布敏感内容(立即撤销该密钥)。2.软件包是
@sendgrid/client
您安装了模块吗?也已弃用,新软件包可在以下位置找到:我现在收到以下错误:ategraham@MacBook-Pro send grid%node sendGrid.js API密钥不以“SG”开头。ResponseError:node_modules/@sendgrid/client/src/classes/client.js:145:29在ProcessTicks和Rejections(node:internal/process/task_queues:93:5)未经授权谢谢-如何创建环境变量?@TateGraham查看。它用于从
.env
文件中创建和读取环境变量
const sgMail = require('@sendgrid/mail')
sgMail.setApiKey(process.env.SENDGRID_API_KEY)