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

Rom*_*ard 3 javascript node.js discord.js

代码:

\n
const { EmbedBuilder } = require("discord.js");\nconst got = require("got");\n\nmodule.exports = {\n  name: "meme",\n  description: "Shows an image of a meme!",\n  run: async (client, message, args) => {\n      got("https://www.reddit.com/r/memes/random/.json").then(response => {\n      const [list] = JSON.parse(response.body);\n      const [post] = list.data.children;\n\n      const permalink = post.data.permalink;\n      const memeUrl = `https://reddit.com${post.data.permalink}`;\n      const memeImage = post.data.url;\n      const memeTitle = post.data.title;\n      const memeUpvotes = post.data.ups;\n      const memeNumComments = post.data.num_comments;\n      \n      const memeEmbed = new EmbedBuilder()\n         .setTitle(`${memeTitle}`)\n         .setURL(`${memeUrl}`)\n         .setColor("Random")\n         .setImage(memeImage)\n         .setFooter({ text: ` ${memeUpvotes} |  ${memeNumComments}` });\n      message.reply({ embeds: [memeEmbed] });\n    });\n  }\n};\n
Run Code Online (Sandbox Code Playgroud)\n

错误:

\n
const got = require("got");\n            ^\nError [ERR_REQUIRE_ESM]: require() of ES Module\n\n/app/node_modules/got/dist/source/index.js from /app/commands/Image/meme.js not supported.\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

有点认为它\xe2\x80\x99s必须对包本身做一些事情,但就像我说的那样,我\xe2\x80\x99m无能为力,因为我\xe2\x80\x99以前从未遇到过这个错误。

\n

Zso*_*ros 6

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

使用ESM

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

  1. 你需要添加"type": "module"到你的package.json
  2. 将所有require()s 和module.exports 替换为importandexport

降级got

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

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