mailgun.js icon indicating copy to clipboard operation
mailgun.js copied to clipboard

TypeError: Mailgun is not a constructor

Open bhkangw opened this issue 1 year ago • 5 comments

Getting Unhandled Runtime Error below.. TypeError: Mailgun is not a constructor

const formData = require("form-data");
const Mailgun = require("mailgun.js");
const mailgun = new Mailgun(formData);

const mg = mailgun.client({
	username: "api",
	key: process.env.MAILGUN_API_KEY,
});

Only way I've been able to bypass is writing imports like this, but then it requires my key or else gives Error: Parameter "key" is required

import * as FormData from "form-data";
import Mailgun from "mailgun.js";
const mailgun = new Mailgun(FormData);

const mg = mailgun.client({ 
	username: "api", 
	key: process.env.MAILGUN_API_KEY || "mykeyhere"
});

bhkangw avatar Sep 07 '23 21:09 bhkangw

Hi @bhkangw Can you add more details about environment and versions of dependencies you use? The second case is really weird just because there is a simple check behind. https://github.com/mailgun/mailgun.js/blob/970c9b66e067cb39d307b2b771bc7ae6f2a5fd2b/lib/Classes/MailgunClient.ts#L64C5-L66C6

olexandr-mazepa avatar Sep 14 '23 13:09 olexandr-mazepa

@olexandr-mazepa thanks for the reply, here is my package.json

{
  "name": "",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@vercel/analytics": "^1.0.2",
    "axios": "^1.5.0",
    "eslint": "8.47.0",
    "eslint-config-next": "13.4.19",
    "form-data": "^4.0.0",
    "mailgun.js": "^9.2.1",
    "micro": "^10.0.1",
    "mongodb": "^6.0.0",
    "mongoose": "^7.5.0",
    "next": "13.4.19",
    "nodemailer": "^6.9.5",
    "prettier": "^3.0.2",
    "prop-types": "^15.8.1",
    "react": "18.2.0",
    "react-dom": "18.2.0"
  },
  "devDependencies": {
    "@types/node": "20.5.3",
    "@types/react": "18.2.21",
    "autoprefixer": "^10.4.15",
    "daisyui": "^3.5.1",
    "postcss": "^8.4.28",
    "tailwindcss": "^3.3.3"
  }
}

bhkangw avatar Sep 14 '23 17:09 bhkangw

Same here

marclou avatar Oct 21 '23 04:10 marclou

Hi @bhkangw @marclou A couple of times I've tried to reproduce this, but unfortunately without any luck. Can somebody share with me a minimum reproducible example for this?

olexandr-mazepa avatar Jan 15 '24 14:01 olexandr-mazepa

Seems to be fixed by using import Mailgun from "mailgun.js" rather than const Mailgun = require("mailgun.js")

life-efficient avatar Jan 16 '24 00:01 life-efficient