Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/got/dist/source/index.js from /app/commands/Image/meme.js not supported

Viewed 400

Code:

const { EmbedBuilder } = require("discord.js");
const got = require("got");

module.exports = {
  name: "meme",
  description: "Shows an image of a meme!",
  run: async (client, message, args) => {
      got("https://www.reddit.com/r/memes/random/.json").then(response => {
      const [list] = JSON.parse(response.body);
      const [post] = list.data.children;

      const permalink = post.data.permalink;
      const memeUrl = `https://reddit.com${post.data.permalink}`;
      const memeImage = post.data.url;
      const memeTitle = post.data.title;
      const memeUpvotes = post.data.ups;
      const memeNumComments = post.data.num_comments;
      
      const memeEmbed = new EmbedBuilder()
         .setTitle(`${memeTitle}`)
         .setURL(`${memeUrl}`)
         .setColor("Random")
         .setImage(memeImage)
         .setFooter({ text: ` ${memeUpvotes} |  ${memeNumComments}` });
      message.reply({ embeds: [memeEmbed] });
    });
  }
};

Error:

const got = require("got");
            ^
Error [ERR_REQUIRE_ESM]: require() of ES Module

/app/node_modules/got/dist/source/index.js from /app/commands/Image/meme.js not supported.

I’m not too sure what this ESM is, I’ve never dealt with it before, therefore I don’t know what to do to fix this.

Kinda think it’s got to do something with the package itself, but like I said I’m clueless as I’ve never had this error before.

2 Answers

got is a native ESM and no longer provides a CommonJS export.

Use ESM

To use the latest version, you'll have to convert to ESM:

  1. you need to add "type": "module" to your package.json
  2. replace all require()s and module.exports with import and export

Downgrade got

Another option is to downgrade got to v11.8.3 as it's pretty stable:

You can run npm i got@11.8.3 in your console.

The problem is when you use type="module" instead and try to use something like __dirname you will get an error:

ReferenceError: __dirname is not defined in ES module scope

Related