错误 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require() [重复]

2023-11-29

我正在尝试做一个Discord机器人只会告诉你是否有人在线玩游戏。

然而我不断收到这样的消息:

[ERR_REQUIRE_ESM]:不支持 ES 模块的 require()。相反,将index.js in... 的 require 更改为动态 import(),该动态 import() 在所有 CommonJS 模块中都可用。

这是我的代码:

    module.exports = {
        name: 'username',
        description: "this is the username command",
        async execute(message, args) {

            const fetch = require('node-fetch');

            if (args.length !== 1) {
                return message.channel.send("invalid username wtf")
            }

            const ign = args[0]

            if (ign.length > 16 || ign.length < 3) {
                return message.channel.send("invalid username wtf")
            }

            const uuid = await fetch(`https://api.mojang.com/users/profiles/minecraft/${ign}`).then(data => data.json()).then(data => data.id).catch(err => message.channel.send("error wtf"));
            const onlineInfo = await fetch(`https://api.hypixel.net/status?key=${john}&uuid=${uuid}`).then(data => data.json());

            if (uuid.length !== 32) {
                return;
            }

            if (onlineinfo.success) {
                if (onlineinfo.session.online) {
                    message.channel.send("they are online")
                }
                else {
                    message.channel.send("they are offline")
                }
            }
            else {
                message.channel.send("hypixel api bad wtf")
            }
        }
    }

这是我的包.json file:

{
    "name": "discordbot",
    "version": "1.0.0",
    "main": "main.js",
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "start": "node main.js"
    },
    "author": "",
    "license": "ISC",
    "description": "",
    "dependencies": {
        "discord.js": "^13.0.1",
        "node-fetch": "^3.0.0"
    }
}

The node-fetch最新版本不使用require()导入包的语法。你需要去你的package.json并输入

 { 
   "type": "module",
 }

使用import语法和导入node-fetch,但是你不能使用require对于任何其他包。您需要与import仅声明。

或者您可以使用其他包,例如Got or Axios,可以通过导入require() syntax.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

错误 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require() [重复] 的相关文章

随机推荐