久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何修復必須使用導入來加載 ES 模塊 discord.js

How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
本文介紹了如何修復必須使用導入來加載 ES 模塊 discord.js的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在開發一個機器人,我做了一個烤命令我收到了這個錯誤

I am working on a bot i made a roast command i am getting this error

internal/modules/cjs/loader.js:1089
      throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
      ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: C:UsersacerDocuments	est
ode_modules
ode-fetchsrcindex.js
require() of ES modules is not supported.
require() of C:UsersacerDocuments	est
ode_modules
ode-fetchsrcindex.js from C:UsersacerDocuments	estcommands
oast.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from C:UsersacerDocuments	est
ode_modules
ode-fetchpackage.json.

←[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1089:13)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:937:32)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:778:12)←[39m
←[90m    at Module.require (internal/modules/cjs/loader.js:961:19)←[39m
←[90m    at require (internal/modules/cjs/helpers.js:92:18)←[39m
    at Object.<anonymous> (C:UsersacerDocuments	estcommands
oast.js:3:15)
←[90m    at Module._compile (internal/modules/cjs/loader.js:1072:14)←[39m
←[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:937:32)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:778:12)←[39m {
  code: ←[32m'ERR_REQUIRE_ESM'←[39m

這是我的roast.js

this is my roast.js

const random = require("something-random-on-discord").Random
const oneLinerJoke = require('one-liner-joke');
const fetch = require('node-fetch')
const Discord = require('discord.js')
module.exports = {
    name : 'roast',
    description : 'roasts a user',
    
  async execute(message, args){
       if (!args[0]) return message.channel.send('Invalid Format')
       const mentionedMember = message.guild.mentions.member.first();
       if (!mentionedMember) return message.channel.send('User not found')
        let msg = await message.channel.send('Setting a roast...')

        fetch('http://evilinsult.com/generate_insult.php?lang=en&type=json')
            .then(res => res.json())
            .then(json => {
                message.channel.send(json.insult)
            });
    }

}

這是我的 main.js

this is my main.js

// Import the discord.js module
const Discord = require('discord.js');
const fs = require('fs');
// Create an instance of a Discord client
const client = new Discord.Client();
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
const prefix = "$"

/**
 * The ready event is vital, it means that only _after_ this will your bot start reacting to information
 * received from Discord
 */
client.on('ready', () => {
  console.log('I am ready!');
});
 
client.on('message', message => {
  if (!message.content.startsWith(prefix) || message.author.bot) return;

  const args = message.content.slice(prefix.length).trim().split(/ +/);
  const command = args.shift().toLowerCase();


  if(command === 'pingg'){
    client.commands.get('pingg').execute(message, args);
  }
  if(command === 'roast'){
    client.commands.get('roast').execute(message, args);
  }




  if (!client.commands.has(command)) return;

  try {
      client.commands.get(command).execute(message, args);
  } catch (error) {
      console.error(error);
      message.reply('there was an error trying to execute that command!');
  }
});
 

commandFiles.forEach(file => {
  const command = file.split(/.js$/)[0];
  client.commands.set(command, require(`./commands/${file}`));
});



client.login('censored');

推薦答案

正如 README.md 也...另一種實現 fetch@3 的方法是使用 node v12.20 中支持的異步 import() 導入 node-fetch

As mention in the README.md also... Another way to make fetch@3 happen is to import node-fetch using the async import() supported in node v12.20

const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));

這也適用于 commonjs(下一個請求不會重新導入 node-fetch 因為模塊被緩存)這可以使 Node 進程啟動更快,并且只在需要時延遲加載 node-fetch

this works from commonjs also (the next request won't re-import node-fetch cuz modules gets cached) This can make the Node process boot up faster and only lazy loads the node-fetch when it's needed

這是另一種預加載方式:

here is another way to preload it:

const fetchP = import('node-fetch').then(mod => mod.default)
const fetch = (...args) => fetchP.then(fn => fn(...args))


您不必將您的孔項目轉換為 ESM,只需 b/c 我們做到了.您也可以繼續使用 v2 分支 - 我們將不斷更新 v2 的錯誤/安全問題.但沒有那么多新功能......


You don't necessary have to convert your hole project to ESM just b/c we did it. You can also stay with the v2 branch - we will keep updating v2 with bug/security issues. But not so much with new features...

(還是推薦別人轉ESM doe)

( Still recommend others to switch to ESM doe )

對于只關注 Types 的 TypeScript 用戶,你可以這樣做:import type { Request } from 'node-fetch'(這不會導入或轉譯任何東西——這個打字注解只會被丟棄)

For TypeScript users who are only after the Types, you can do: import type { Request } from 'node-fetch' (this will not import or transpile anything - this typing annotation will just be discarded)

在 #1279

這篇關于如何修復必須使用導入來加載 ES 模塊 discord.js的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
Discord.js Delete Single Message(Discord.js 刪除單個消息)
主站蜘蛛池模板: 中文字幕一区二区三区四区不卡 | 亚洲黄色在线免费观看 | 久草中文在线 | 自拍第一页| 在线免费观看毛片 | 91亚洲精品国偷拍自产在线观看 | 久久久久一区 | 毛色毛片免费看 | 国内精品久久久久久久 | 亚洲一区视频在线 | 国产精品成人一区二区 | 亚欧午夜| 久久亚洲美女 | 亚洲高清免费 | 超碰在线国产 | 久久在线看| 九九九视频在线观看 | 日批免费看 | 欧美一区二区三区在线观看视频 | 日韩精品 电影一区 亚洲 | 精品视频在线观看 | 视频一区中文字幕 | 亚洲一二三区不卡 | 久久久久久久久久久福利观看 | 国产美女在线精品免费 | 国内久久精品 | 亚洲一区二区中文字幕在线观看 | 国产免费a视频 | 热久久久 | 毛片国产 | 亚洲精品久久久一区二区三区 | 免费看黄色视屏 | 女人精96xxx免费网站p | 久久久久久久亚洲精品 | 一区在线播放 | 亚洲人成一区二区三区性色 | 9久9久9久女女女九九九一九 | 亚洲成人一区二区三区 | 黄色片在线 | 中文字幕在线一区 | 天天天操 |