I have been trying to run an app where I can set up an email.
When I run it on the localhost, I get a 202 response, but I don’t get an email. I conclude that this is because it is the localhost and conclude I should upload it on cloudflare and set up a domain. I do this, however, when I look at the deployment details, I get this error.
TypeError [ERR_INVALID_ARG_TYPE]: The “path” argument must be of type string. Received undefined
I have the code here.
import url from "node:url";
import path from "path";
import { fileURLToPath } from "url";
//Not sure why the value is never read.
import express from "express";
import bodyParser from "body-parser";
import sgMail from '@sendgrid/mail';
import process from 'node:process';
import dotenv from 'dotenv';
dotenv.config();
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = import.meta.filename;
//const PORT = 3000; // Defining PORT
//This was for the localhost.
const apikey = process.env.API_KEY;
const app = express();
app.use(express.static(path.join(__dirname)));
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, import.meta.url));
res.sendFile(path.join(__dirname, "index.html"));
res.sendFile(path.join(__dirname, "sendgridpart2.css"));
res.end();
});
app.post("/submit", (req, res) => {
console.log(`${apikey} is the apikey`)
sgMail.setApiKey(apikey)
const msg = {
to: req.body.To,
from: req.body.From,
subject: req.body.Subject,
text: req.body.Message,
html: req.body.Message,
};
sgMail
.send(msg)
.then((response) => {
console.log(response[0].statusCode);
console.log(response[0].headers);
})
.catch((error) => {
console.error(error);
});
});
{
"name": "sendgridpart2",
"version": "1.0.0",
"description": "",
"main": "app.js",
"type": "module",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@sendgrid/mail": "^8.1.3",
"body-parser": "^1.20.2",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"node": "^22.4.0",
"nodemon": "^3.1.4"
}
}
I have attempted to find a way to fix the code. One error, I noticed earlier was that I still had the localhost functioning. I commented it out. Additionally, I saw an error that I had
const __dirname = import.meta.firname; . I assumed this was causing the issue with making the path a string, but that didn’t change anything. Thank you for the help.