Here is the website: https://atlanticcabinetrefacing.netlify.app/
Here is my function that sends my emails through sendgrid. When I run it in local host, everything works perfectly fine.
import axios from 'axios';
export async function handler(event, context) {
if (event.body === null) {
return {
statusCode: 400,
body: JSON.stringify("Payload required"),
};
}
const formData = JSON.parse(event.body);
try {
const response = await axios.post(`${process.env.URL}/.netlify/functions/emails/sendGridEmail`, {
from: "placeholder@gmail.com",
to: "placeholder@gmail.com",
subject: "New Estimate Request",
parameters: {
name: formData.name,
city: formData.city,
state: formData.state,
homePhone: formData.homePhone,
cellPhone: formData.cellPhone,
email: formData.email,
},
}, {
headers: {
"netlify-emails-secret": process.env.NETLIFY_EMAILS_SECRET,
},
});
if (!response.status === 200) {
return {
statusCode: response.status,
body: JSON.stringify(`Email failed to send: ${response.statusText}`),
};
}
return {
statusCode: 200,
body: JSON.stringify("Email sent!"),
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify(`Server error: ${error}`)
};
}
};
I get this error
Runtime.ImportModuleError: Error: Cannot find module '/var/task/node_modules/axios/dist/node/axios.cjs'
at _loadUserApp (file:///var/runtime/index.mjs:996:17)
at async Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1031:21)
at async start (file:///var/runtime/index.mjs:1194:23)
at async file:///var/runtime/index.mjs:1200:1
However, it is clearly in my directory. The only thing that I think could be causing a problem would be that it starts the path with /var/task, and that is not in my normal directory. Either way, it is odd that it works through the netlify cli on my desktop and laptop but fails when it is live.
I have looked through this and this
However, this one may be the solution if I need to have a package.json inside of my functions folder since my directory looks like this currently
If I need to add a package.json, I’m not exactly sure how I would structure it.
I had also tried using node-fetch instead of axios, but I kept getting this error in production, which seemed to boil down to not being able to access fetch-blob. But this issue persisted even when I removed all references of fetch-blob from my files. So if there is a way to either alter it so that axios works or to go back to node-fetch that works without fetch-blob errors I would appreciate it greatly.
Apr 18, 05:08:14 PM: 257e3434 2023-04-18T21:08:14.830Z undefined ERROR Uncaught Exception {“errorType”:“TypeError”,“errorMessage”:“Class extends value # is not a constructor or null”,“stack”:[“TypeError: Class extends value # is not a constructor or null”," at Object. (/var/task/node_modules/fetch-blob/file.js:53:18)“,” at Module._compile (node:internal/modules/cjs/loader:1165:14)“,” at Object.Module._extensions…js (node:internal/modules/cjs/loader:1219:10)“,” at Module.load (node:internal/modules/cjs/loader:1043:32)“,” at Function.Module._load (node:internal/modules/cjs/loader:878:12)“,” at Module.require (node:internal/modules/cjs/loader:1067:19)“,” at require (node:internal/modules/cjs/helpers:103:18)“,” at Object. (/var/task/node_modules/formdata-polyfill/esm.min.js:29:27)“,” at Module._compile (node:internal/modules/cjs/loader:1165:14)“,” at Object.Module._extensions…js (node:internal/modules/cjs/loader:1219:10)“]}Apr 18, 05:08:14 PM: 257e3434 2023-04-18T21:08:15.111Z undefined ERROR Uncaught Exception {“errorType”:“TypeError”,“errorMessage”:“Class extends value # is not a constructor or null”,“stack”:[“TypeError: Class extends value # is not a constructor or null”,” at Object. (/var/task/node_modules/fetch-blob/file.js:53:18)“,” at Module._compile (node:internal/modules/cjs/loader:1165:14)“,” at Object.Module._extensions…js (node:internal/modules/cjs/loader:1219:10)“,” at Module.load (node:internal/modules/cjs/loader:1043:32)“,” at Function.Module._load (node:internal/modules/cjs/loader:878:12)“,” at Module.require (node:internal/modules/cjs/loader:1067:19)“,” at require (node:internal/modules/cjs/helpers:103:18)“,” at Object. (/var/task/node_modules/formdata-polyfill/esm.min.js:29:27)“,” at Module._compile (node:internal/modules/cjs/loader:1165:14)“,” at Object.Module._extensions…js (node:internal/modules/cjs/loader:1219:10)"]}Apr 18, 05:08:14 PM: 257e3434 Unknown application error occurredApr 18, 05:08:14 PM: 257e3434 Runtime.Unknown