Home > Software design >  Fetch is not Defined
Fetch is not Defined

Time:04-07

I'm very new to Javascript and just built my very first bot, I try to run my bot and it WORKS! But when I use the discord command it crashes on Repl.it. I have done npm install node-fetch but that doesn't seem to be working either. Here is the error:

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/runner/myrepl/node_modules/node-fetch/src/index.js from /home/runner/myrepl/index.js not supported.
Instead change the require of /home/runner/myrepl/node_modules/node-fetch/src/index.js in /home/runner/myrepl/index.js to a dynamic import() which is available in all CommonJS modules.
    at Client.<anonymous> (/home/runner/myrepl/index.js:52:21)
    at Client.emit (node:events:390:28)
    at Client.emit (node:domain:475:12)
    at MessageCreateAction.handle (/home/runner/myrepl/node_modules/discord.js/src/client/actions/MessageCreate.js:26:14)
    at Object.module.exports [as MESSAGE_CREATE] (/home/runner/myrepl/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (/home/runner/myrepl/node_modules/discord.js/src/client/websocket/WebSocketManager.js:351:31)
    at WebSocketShard.onPacket (/home/runner/myrepl/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (/home/runner/myrepl/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (/home/runner/myrepl/node_modules/ws/lib/event-target.js:199:18)
    at WebSocket.emit (node:events:390:28)
    at WebSocket.emit (node:domain:475:12)
    at Receiver.receiverOnMessage (/home/runner/myrepl/node_modules/ws/lib/websocket.js:1137:20)
    at Receiver.emit (node:events:390:28)
    at Receiver.emit (node:domain:475:12)
    at Receiver.dataMessage (/home/runner/myrepl/node_modules/ws/lib/receiver.js:528:14)
    at Receiver.getData (/home/runner/myrepl/node_modules/ws/lib/receiver.js:446:17)
    at Receiver.startLoop (/home/runner/myrepl/node_modules/ws/lib/receiver.js:148:22)
    at Receiver._write (/home/runner/myrepl/node_modules/ws/lib/receiver.js:83:10)
    at TLSSocket.socketOnData (/home/runner/myrepl/node_modules/ws/lib/websocket.js:1231:35)
    at TLSSocket.emit (node:events:390:28)
    at TLSSocket.emit (node:domain:475:12) {
  code: 'ERR_REQUIRE_ESM'
}
repl process died unexpectedly: exit status 1

My Index.js is:

    node-fetch(`https://www.rolimons.com/uaid/`   id).then(res => res.text()).then(res => {
      //// clog(res)
      

I am SO SO CLOSE to completing my very first bot please help! Any help is appreciated. Thanks!

CodePudding user response:

Here is the solution to avoid esm issues with node-fetch.

npm i node-fetch@2

CodePudding user response:

It was hard to find, but after alot of searching I found a way it would work. This worked:

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