Im having an annoying problem which i’ve spent most of my day trying to solve without any luck – Also tried searching for a solution here and on Google, but without any luck, or at least all solution tried out didn’t work.
When running my function, the firebase function log is telling me:
Cannot find module 'mailgun.js'
I have no idea why it won’t resolve the module as it exist in the package.json file.
From the package.json:
"dependencies": {
"cors": "^2.8.5",
"firebase-admin": "^9.8.0",
"firebase-functions": "^3.14.1",
"form-data": "^4.0.0",
"mailgun.js": "^4.1.4"
},
I’ve have done a npm install inside the functions folder, and it deploys without problems, so the problem is when calling the function…
The code is:
import * as functions from "firebase-functions";
import Mailgun from "mailgun.js";
// eslint-disable-next-line @typescript-eslint/no-var-requires
const formData = require("form-data");
const domain = "https://app.mailgun.com/app/sending/domains/sandboxae7cd087b3854d25a6933f1fe489b5e3.mailgun.org";
const mgClient = new Mailgun(formData);
const mg = mgClient.client({
key: "mykey",
username: "api",
public_key: "mypublickey",
});
export const sendWelcomeEmailToClient = functions.https.onCall(
async (email: string) => {
return mg.messages.create(domain, {
from: "Task System",
to: email,
subject: "Testing",
html: "Dette er en test",
});
}
);
Please note, i’ve also tried doing a require instead of import on mailgun.js like so, but didn’t work either:
const Mailgun = require("mailgun.js");