我正在开发一个机器人,我做了一个烤命令我收到了这个错误
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!