繁体   English   中英

错误 [ERR_REQUIRE_ESM]:不支持来自 /app/commands/Image/meme.js 的 ES 模块 /app/node_modules/got/dist/source/index.js 的 require()

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

代码:

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] });
    });
  }
};

错误:

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.

我不太确定这个 ESM 是什么,我以前从未处理过它,因此我不知道该怎么做才能解决这个问题。

有点认为它必须对 package 本身做些什么,但就像我说的那样我很无能,因为我以前从未遇到过这个错误。

got是原生 ESM,不再提供 CommonJS 导出。

使用 ESM

要使用最新版本,您必须转换为 ESM:

  1. 您需要在package.json中添加"type": "module"
  2. importexport替换所有require()module.export

降级got

另一种选择是将got降级到v11.8.3 ,因为它非常稳定:

您可以在控制台中运行npm i got@11.8.3

问题是当您改用 type="module" 并尝试使用类似__dirname的内容时,您将收到ReferenceError: __dirname is not defined in ES module scope

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM