question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SyntaxError: Unexpected token M in JSON at position 0

See original GitHub issue

Here the complete error:

SyntaxError: Unexpected token M in JSON at position 0
    at JSON.parse (<anonymous>)
    at x.json (/workspace/node_modules/mailgun.js/dist/mailgun.js:2:37391)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

This is my code:

import Mailgun from 'mailgun.js';

const MAILGUN_KEY: string = functions.config().mailgun.key;
const MAILGUN_URL: string = functions.config().mailgun.url;
const MAILGUN_DOMAIN: string = functions.config().mailgun.domain;
const mailgun = new Mailgun(require('form-data'));
const mg = mailgun.client({
  username: 'api',
  key: MAILGUN_KEY,
  url: MAILGUN_URL,
});

..........

await mg.messages.create(MAILGUN_DOMAIN, {
      to: [to],
      from: `****- ****<${from}>`,
      bcc: '****@****',
      subject,
      html,
    });

I checked everything in my configuration, I have no problem with sendgrid js api so no network problem. I am using node 12, "mailgun.js": "^3.3.0",

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
scaccogattocommented, Apr 15, 2021

I see, a simple misconfiguration can bring you this kind of error. I think it should be easier to understand. I will look into the code to make this thing simpler, hope soon.

For anyone looking for an answer: check your configuration.

2reactions
SixBytesUndercommented, Apr 12, 2022

Oh my god, it’s working! So the documentation is wrong. If you see here https://documentation.mailgun.com/en/latest/quickstart-sending.html#send-via-api and click on Node.js, you can clearly see right under the code example NOTE: If you’re sending from our EU infrastructure, be sure to substitute our EU endpoint in the above example: https://api.eu.mailgun.net/v3 So that’s what I used and assumed this is correct. Thank you @olexandr-mazepa you’re a star 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unexpected token m in JSON at position 0 error - Stack Overflow
The reason it says unexpected token m is because the json-loader transforms your json into a javascript file which starts with ...
Read more >
What Is JSON and How to Handle an “Unexpected Token” Error
“Unexpected token < in JSON at position 0” is the error that I have seen most throughout my time of working as a...
Read more >
How to Fix SyntaxError: Unexpected token < in JSON at ...
This guide will help to fix SyntaxError: Unexpected token < in JSON at position 0 . This guide also applies to these other...
Read more >
Unexpected token in JSON at position 0 - Moodle.org
Re: Unexpected token in JSON at position 0 ... This usually means that an error has been returned and that's not valid JSON....
Read more >
How To Fix Unexpected token u in JSON at position 0 - Isotropic
The "Unexpected token u in JSON at position 0" is a typically Javascript error that will show up in your console log if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found