TypeError: fetch is not a function

See original GitHub issue

my code was just working at least 5 hours ago and now is giving me this error

TypeError: fetch is not a function
    at Object.<anonymous> (D:\Projects\PaintBot\libs\mc.js:2:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (D:\Projects\PaintBot\commands\about.js:2:16)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
const fetch = import('node-fetch')
const getUUID = async function(un){
    let uuid
    await fetch(`https://api.mojang.com/users/profiles/minecraft/${un}`)
    .then(res=>res.json())
    .then(json=>{
        try {
            uuid = {result: true, uuid: json.id}
        } catch {
            uuid = {result: false, uuid: null}
        }
    })
    return uuid
}

this code is supposed to just return the result and id but instead, it says fetch isn’t a function, am I doing something wrong?

My Environment

software version
node-fetch 3.1.0
node 16.11.1
npm 8.0.0
Operating System Microsoft Windows [Version 10.0.19044.1348]

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mavthedevcommented, Dec 7, 2021

Fixed! Here is the code:

const fetch = import('node-fetch')
const getUUID = async function(un){
    let uuid
    await fetch.then(async data=>{
        await data.default(`https://api.mojang.com/users/profiles/minecraft/${un}`)
        .then(res=>res.json())
        .then(json=>{
            try {
                uuid = {result: true, uuid: json.id}
            } catch {
                uuid = {result: false, uuid: null}
            }
        })
    })
    return uuid
}
0reactions
jimmywartingcommented, Dec 7, 2021

vscode (typescript) will have a harder time of knowing what the return type is if you assign uuid later like that… it’s better to have a return instead.

few examples if you want to keep import at top:

const fetch = import('node-fetch')

const getUUID = async function(un){
    const uuid = await fetch.then(async data=>{
        return data.default(`https://api.mojang.com/users/profiles/minecraft/${un}`)
        .then(res=>res.json())
        .then(json=>{
            try {
              return { result: true, uuid: json.id }
            } catch {
              return { result: false, uuid: null }
            }
        })
    })
    return uuid
}
const fetch = import('node-fetch')

const getUUID = function(un){
  return fetch.then(data => {
    return data.default(`https://api.mojang.com/users/profiles/minecraft/${un}`)
    .then(res => res.json())
    .then(json => {
      try {
        return { result: true, uuid: json.id }
      } catch {
        return { result: false, uuid: null }
      }
    })
  })
}
const fetch = import('node-fetch')

const getUUID = un => fetch
  .then(({default: fetch}) => fetch(`https://api.mojang.com/users/profiles/minecraft/${un}`))
  .then(res => res.json())
  .then(json => {
    try {
      return { result: true, uuid: json.id }
    } catch {
      return { result: false, uuid: null }
    }
  })
const fetch = import('node-fetch')

const getUUID = un => fetch
  .then(({ default: fetch }) => fetch(`https://api.mojang.com/users/profiles/minecraft/${un}`))
  .then(res => res.json())
  .then(json => ({ result: true, uuid: json.id }))
  // fallback if any of the above fails (load module, parse json, or get id of json)
  .catch(err => ({ result: false, uuid: null }))
const fetch = import('node-fetch')

const getUUID = async function (un) {
  const url = `https://api.mojang.com/users/profiles/minecraft/${un}`
  const { default: _fetch } = await fetch
  const res = await _fetch(url)
  const json = await res.json()

  try {
    return { result: true, uuid: json.id }
  } catch (err) {
    return { result: false, uuid: null }
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

(CLOSED) TypeError: fetch is not a function, What did I do ...
Try using this style of import: import fetch from 'node-fetch';.
Read more >
fetch is not a function · Issue #271 - GitHub
Hello, I'm using fetch from npm packages and when i run the code, it says fetch is not a function. Here is my...
Read more >
ReferenceError: fetch is not defined in NodeJs | bobbyhadz
The "ReferenceError: fetch is not defined" occurs when the fetch() method is used in an environment where it's not supported - most commonly...
Read more >
fetch fails in page's load function when rendered on the server
It's a weird thing with localhost. Try using 'http://127.0.0.1:1337/api/posts?populate=*' Hopefully the server you are fetching from is allowing ...
Read more >
node-fetch - npm
A light-weight module that brings Fetch API to node.js. Latest version: 3.3.0, last published: 2 months ago. Start using node-fetch in your ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found