(Closed) Typeerror: Fetch Is Not a Function, What Did I Do Wrong?

(Closed) Typeerror: Fetch Is Not a Function, What Did I Do Wrong?

I'm trying to use node-fetch in order to fetch this website that has this JSON file where I can use it for my discord bot.

Code (JS):

const fetch = import("node-fetch")

// some code until

data = ""
        try {
            data = await fetch(`()})}`).then(res => res.json)
            errored = false
        } catch (error) {
            throw error;
        }

It errors saying:

TypeError: fetch is not a function

How do I fix this?

FYI, Using require() resulted in an error as such:

const fetch = require("node-fetch")
              ^

Error [ERR_REQUIRE_ESM]: require() of ES Module F:\Users\Basil Atif\Folders\VsauceBot\node_modules\node-fetch\src\index.js from F:\Users\Basil Atif\Folders\VsauceBot\src\Commands\reddit.js not supported.
Instead change the require of index.js in F:\Users\Basil Atif\Folders\VsauceBot\src\Commands\reddit.js to a dynamic import() which is available in all CommonJS modules.    
  code: 'ERR_REQUIRE_ESM'
}
5

1 Answer

Try using this style of import:

import fetch from 'node-fetch';
8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.