【问题标题】:错误 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require() [重复]
【发布时间】:2021-11-03 22:46:39
【问题描述】:

我正在尝试制作一个不和谐的机器人,它只会显示是否有人在游戏中在线。

但我不断收到此消息:

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

这是我的代码:

    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")
            }
        }
    }

这是我的 package.json:

{
    "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"
    }
}

【问题讨论】:

  • 我没有所有 module.exports 的东西,我需要吗?我只是说出口类什么的,有错吗?

标签: javascript node.js discord.js node-fetch hypixel-api


【解决方案1】:

node-fetch 最新版本不使用require() 语法来导入包。您需要转到您的package.json 并输入

 { 
   "type": "module",
 }

使用import 语法并导入node-fetch,但是您不能将require 用于您需要仅使用import 语句的任何其他包。

或者您可以使用其他包,例如GotAxios,可以通过require() 语法导入。

【讨论】:

  • 我在代码的不同文件中使用了 require(),那么它是如何工作的呢?
  • 我已经这样做了,但我仍然收到错误,因为打字稿转译器将要求放回 .js 中。我如何让它停止这样做?
  • 不起作用,仍然抛出错误 [ERR_REQUIRE_ESM]: require() of ES Module not supported。虽然我的代码中没有一个 require() 了。
【解决方案2】:

node-fetch v3 最近停止了对 require 导入方式的支持,转而支持 ES 模块。您现在需要使用 ESM 导入,例如:

import fetch from "node-fetch";

在文件的顶部。

【讨论】:

【解决方案3】:

我想通了。我只好将node-fetch 降级到2.6.1,因为更高版本只使用ESM,这导致了很多错误。

【讨论】:

  • 当我尝试这样做时,我从打字稿中得到编译时错误,比如它无法导入节点提取,找不到节点提取,找不到节点提取的类型定义, blah blah blah ...然后如果我添加类型定义,它说我不需要那些... wtf 一直在使用 typescript 和 node
【解决方案4】:

只需替换文件顶部的需要导入即可。

// const fetch = require('node-fetch');
import fetch from "node-fetch";

node-fetch v3 最近停止了对 require 导入方式的支持,转而支持 ES 模块。您现在需要使用 ESM imports

使用降级 node-fetch 到 2.6.1,因为更高版本只使用 EMS

【讨论】:

    猜你喜欢
    • 2022-11-09
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 2022-08-23
    • 2022-11-18
    • 2021-12-07
    • 2022-11-23
    • 2021-12-28
    相关资源
    最近更新 更多