Reputation: 27
I keep getting an 'Error: Cannot find module 'sendgrid' in my console when attempting to execute my code. I have never used sendgrid before, am I doing something wrong?
SENDGRID =
"<key>";
const sgMail = require("sendgrid").SendGrid(conf);
sgMail.setApiKey(process.env.SENDGRID);
const msg = {
to: "[email protected]", // Change to your recipient
from: "[email protected]", // 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 %
Upvotes: 0
Views: 2411
Reputation: 139
const sgMail = require('@sendgrid/mail')
sgMail.setApiKey(process.env.SENDGRID_API_KEY)
Here is a link to actual doc: https://sendgrid.com/docs/for-developers/sending-email/quickstart-nodejs/#complete-code-block
Update On Using The Environment Variable
First create .env file something like sendgrid.env. Then you can use require('dotenv').config(sendgrid.env)
or maybe, just
require('dotenv').config(sendgrid)
Then you can use SENDGRID_API_KEY defined in that env file At least this can be done according to my understanding to your question.
Upvotes: 1