0

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!

user18383426
  • 121
  • 2
  • 8
  • If this is nodeJs code it won't understand fetch. You'll have to [import something like `node-fetch`](https://www.npmjs.com/package/node-fetch) and use that. Note: the fetch API [is coming to node soon](https://blog.logrocket.com/fetch-api-node-js/). – Andy Apr 05 '22 at 19:58
  • How would I do that @Andy ? – user18383426 Apr 05 '22 at 19:58
  • did you forget to `import fetch from 'node-fetch';`? – Robin Zigmond Apr 05 '22 at 20:00
  • @RobinZigmond I have tried that but it keeps saying "Cannot import outside of module" – user18383426 Apr 05 '22 at 20:10
  • Then try `const fetch = require('node-fetch');` instead of `import` – cachius Apr 05 '22 at 20:16
  • Gives a big error: `Error [ERR_REQUIRE_ESM]: require() of ES Module /home/runner/Poisithink/node_modules/node-fetch/src/index.js from /home/runner/Poisithink/index.js not supported. Instead change the require of /home/runner/Poisithink/node_modules/node-fetch/src/index.js in /home/runner/Poisithink/index.js to a dynamic import() which is available in all CommonJS modules` @cachius (There is much more lol) – user18383426 Apr 05 '22 at 20:22
  • 5
    Latest edit constitutes duplicate of [Error: require() of ES modules is not supported when importing node-fetch](https://stackoverflow.com/questions/69041454/error-require-of-es-modules-is-not-supported-when-importing-node-fetch) – esqew Apr 05 '22 at 20:26

2 Answers2

2

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));
user18383426
  • 121
  • 2
  • 8
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 06 '22 at 13:06
1

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

npm i node-fetch@2
Lemaaa
  • 90
  • 1
  • 9