-1

I have a code which I'm doing on replit, and I'm pretty new to node.js so I really do need assistance with this. I have a code which is this:

const Discord = require("discord.js")
const fetch = require("node-fetch")
const client = new Discord.Client()
const mySecret = process.env['TOKEN']

function getQuote() {
    return fetch('https://zenquotes.io/api/random')
     .then(res => {
         return res.json
     })
     .then(data => {
         return data[0]["q"] + " —" +  data [0]["a"]
     })



}

client.on("ready", () => {
    console.log('Logged in as ${client.user.tag}' )
})

client.on("message", msg => {
    if (msg.author.bot) return



    if (msg.content === "$inspire") {
        getQuote().then(quote => msg.channel.send(quote))

    }
})


client.login(process.env.TOKEN)

However, I'm receiving the error:

npx node .
/home/runner/1Starty/index.js:2
const fetch = require("node-fetch")
              ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/runner/1Starty/node_modules/node-fetch/src/index.js from /home/runner/1Starty/index.js not supported.
Instead change the require of /home/runner/1Starty/node_modules/node-fetch/src/index.js in /home/runner/1Starty/index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/home/runner/1Starty/index.js:2:15) {
  code: 'ERR_REQUIRE_ESM'
}

May someone help me on why this is the case?

Exotic
  • 19
  • 4
  • Does this answer your question? [require('node-fetch') gives ERR\_REQUIRE\_ESM](https://stackoverflow.com/questions/69087292/requirenode-fetch-gives-err-require-esm) – Zsolt Meszaros Jan 12 '22 at 20:28

1 Answers1

1

Open shell and run this command

npm i node-fetch@2.6.6
Turtlepaw
  • 380
  • 1
  • 9
Patrick
  • 51
  • 5