簡體   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