pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

如何修復(fù)必須使用導(dǎo)入來(lái)加載 ES 模塊 discord.js

How to fix Must use import to load ES Module discord.js(如何修復(fù)必須使用導(dǎo)入來(lái)加載 ES 模塊 discord.js)
本文介紹了如何修復(fù)必須使用導(dǎo)入來(lái)加載 ES 模塊 discord.js的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在開(kāi)發(fā)一個(gè)機(jī)器人,我做了一個(gè)烤命令我收到了這個(gè)錯(cuò)誤

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 也...另一種實(shí)現(xiàn) fetch@3 的方法是使用 node v12.20 中支持的異步 import() 導(dǎo)入 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(下一個(gè)請(qǐng)求不會(huì)重新導(dǎo)入 node-fetch 因?yàn)槟K被緩存)這可以使 Node 進(jìn)程啟動(dòng)更快,并且只在需要時(shí)延遲加載 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

這是另一種預(yù)加載方式:

here is another way to preload it:

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


您不必將您的孔項(xiàng)目轉(zhuǎn)換為 ESM,只需 b/c 我們做到了.您也可以繼續(xù)使用 v2 分支 - 我們將不斷更新 v2 的錯(cuò)誤/安全問(wèn)題.但沒(méi)有那么多新功能......


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...

(還是推薦別人轉(zhuǎn)ESM doe)

( Still recommend others to switch to ESM doe )

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

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

這篇關(guān)于如何修復(fù)必須使用導(dǎo)入來(lái)加載 ES 模塊 discord.js的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

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(如何讓我的機(jī)器人提及發(fā)出該機(jī)器人命令的人)
How to list all members from a specific server?(如何列出來(lái)自特定服務(wù)器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復(fù)“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務(wù)器時(shí)的歡迎消息)
Discord.js Delete Single Message(Discord.js 刪除單個(gè)消息)
主站蜘蛛池模板: 交通信号灯生产厂家_红绿灯厂家_电子警察监控杆_标志杆厂家-沃霖电子科技 | 烟气换热器_GGH烟气换热器_空气预热器_高温气气换热器-青岛康景辉 | 体检车_移动CT车_CT检查车_CT车_深圳市艾克瑞电气有限公司移动CT体检车厂家-深圳市艾克瑞电气有限公司 | 生鲜配送系统-蔬菜食材配送管理系统-连锁餐饮订货配送软件-挪挪生鲜供应链管理软件 | 刑事律师_深圳著名刑事辩护律师_王平聚【清华博士|刑法教授】 | FAG轴承,苏州FAG轴承,德国FAG轴承-恩梯必传动设备(苏州)有限公司 | 真空干燥烘箱_鼓风干燥箱 _高低温恒温恒湿试验箱_光照二氧化碳恒温培养箱-上海航佩仪器 | 3D全息投影_地面互动投影_360度立体投影_水幕灯光秀 | 广东银虎 蜂窝块状沸石分子筛-吸附脱硫分子筛-萍乡市捷龙环保科技有限公司 | 不锈钢复合板|钛复合板|金属复合板|南钢集团安徽金元素复合材料有限公司-官网 | 清水-铝合金-建筑模板厂家-木模板价格-铝模板生产「五棵松」品牌 | 户外环保不锈钢垃圾桶_标识标牌制作_园林公园椅厂家_花箱定制-北京汇众环艺 | 炉门刀边腹板,焦化设备配件,焦化焦炉设备_沧州瑞创机械制造有限公司 | 防爆电机生产厂家,YBK3电动机,YBX3系列防爆电机,YBX4节防爆电机--河南省南洋防爆电机有限公司 | 手持式线材张力计-套帽式风量罩-深圳市欧亚精密仪器有限公司 | 无线遥控更衣吊篮_IC卡更衣吊篮_电动更衣吊篮配件_煤矿更衣吊篮-力得电子 | 瓶盖扭矩仪(扭力值检测)-百科| 无线联网门锁|校园联网门锁|学校智能门锁|公租房智能门锁|保障房管理系统-KEENZY中科易安 | 滚塑PE壳体-PE塑料浮球-警示PE浮筒-宁波君益塑业有限公司 | 广州印刷厂_广州彩印厂-广州艺彩印务有限公司 | 工业车间焊接-整体|集中除尘设备-激光|等离子切割机配套除尘-粉尘烟尘净化治理厂家-山东美蓝环保科技有限公司 | ZHZ8耐压测试仪-上海胜绪电气有限公司 | 小学教案模板_中学教师优秀教案_高中教学设计模板_教育巴巴 | 深圳市宏康仪器科技有限公司-模拟高空低压试验箱-高温防爆试验箱-温控短路试验箱【官网】 | 无硅导热垫片-碳纤维导热垫片-导热相变材料厂家-东莞市盛元新材料科技有限公司 | 优考试_免费在线考试系统_培训考试系统_题库系统_组卷答题系统_匡优考试 | 十字轴_十字轴万向节_十字轴总成-南京万传机械有限公司 | 铝机箱_铝外壳加工_铝外壳厂家_CNC散热器加工-惠州市铂源五金制品有限公司 | 隔爆型防爆端子分线箱_防爆空气开关箱|依客思 | 贵州水玻璃_-贵阳花溪闽兴水玻璃厂 | 上海平衡机-单面卧式动平衡机-万向节动平衡机-圈带动平衡机厂家-上海申岢动平衡机制造有限公司 | 飞象网 - 通信人每天必上的网站| 服务器之家 - 专注于服务器技术及软件下载分享 | 焦作网 WWW.JZRB.COM | 最新电影-好看的电视剧大全-朝夕电影网| 智慧钢琴-电钢琴-便携钢琴-数码钢琴-深圳市特伦斯乐器有限公司 | 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | 深圳美安可自动化设备有限公司,喷码机,定制喷码机,二维码喷码机,深圳喷码机,纸箱喷码机,东莞喷码机 UV喷码机,日期喷码机,鸡蛋喷码机,管芯喷码机,管内壁喷码机,喷码机厂家 | 开云(中国)Kaiyun·官方网站 - 登录入口| pbt头梳丝_牙刷丝_尼龙毛刷丝_PP塑料纤维合成毛丝定制厂_广州明旺 | 南昌旅行社_南昌国际旅行社_南昌国旅在线 | 武汉宣传片制作-视频拍摄-企业宣传片公司-武汉红年影视 |