Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch doesn't work in 19.3.0 #46049

Closed
fawazahmed0 opened this issue Jan 1, 2023 · 14 comments
Closed

Fetch doesn't work in 19.3.0 #46049

fawazahmed0 opened this issue Jan 1, 2023 · 14 comments
Labels
fetch Issues and PRs related to the Fetch API

Comments

@fawazahmed0
Copy link

fawazahmed0 commented Jan 1, 2023

Version

19.3.0

Platform

Microsoft Windows NT 10.0.22000.0 x64

Subsystem

No response

What steps will reproduce the bug?

let url = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/"
let dateValue = 'latest'
async function begin(){
let data = await fetch(`${url}/${dateValue}/currencies.json`).then(res => res.json())
}
begin()

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior?

No response

What do you see instead?

node:internal/deps/undici/undici:14062
    Error.captureStackTrace(err, this);
          ^

TypeError: fetch failed
    at Object.fetch (node:internal/deps/undici/undici:14062:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async begin (C:\Users\nawaz\Desktop\codetest\fec\cache-jsdelivr-api.js:12:20) {
  cause: TypeError: Cannot read properties of undefined (reading 'reason')
      at makeAppropriateNetworkError (node:internal/deps/undici/undici:6736:171)
      at schemeFetch (node:internal/deps/undici/undici:13492:16)
      at node:internal/deps/undici/undici:13422:26
      at mainFetch (node:internal/deps/undici/undici:13439:11)
      at httpRedirectFetch (node:internal/deps/undici/undici:13692:14)
      at httpFetch (node:internal/deps/undici/undici:13638:28)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async schemeFetch (node:internal/deps/undici/undici:13546:18)
      at async node:internal/deps/undici/undici:13422:20
      at async mainFetch (node:internal/deps/undici/undici:13418:20)
}

Node.js v19.3.0

Additional information

The same code works fine in v18.12.1

If you are interested, here is my complete code:

// Fetch all latest & todays links to cache the data by jsdelivr
let url = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/"
let apiLinks = [url]
let extensions = [".min.json", ".json"]


async function begin() {

    for (let dateValue of ['latest', new Date().toISOString().substring(0, 10)]) {


        let data = await fetch(`${url}/${dateValue}/currencies.json`).then(res => res.json())
        let currencies = Object.keys(data)


        await multipleFetch(getURLs(`${url}/${dateValue}/currencies`))

        for (let curr of currencies) {
            let promiseHolder = []

            promiseHolder.push(multipleFetch(getURLs(`${url}/${dateValue}/currencies/${curr}`)))

            for (let curr2 of currencies)
                promiseHolder.push(multipleFetch(getURLs(`${url}/${dateValue}/currencies/${curr}/${curr2}`)))

            await Promise.allSettled(promiseHolder)
        }



    }





}

async function multipleFetch(links) {
    for (let link of links)
        await fetch(link, { method: 'HEAD' })
}

function getURLs(endpoint, links) {
    links = links || apiLinks
    return extensions.map(ext => links.map(e => e + endpoint + ext)).flat()
}

begin()
@KhafraDev
Copy link
Member

KhafraDev commented Jan 2, 2023

This was fixed in nodejs/undici#1787 and undici was updated in #45812, it doesn't appear as though it made it into v19.3.0 though.

@VoltrexKeyva VoltrexKeyva added the fetch Issues and PRs related to the Fetch API label Jan 2, 2023
@fawazahmed0
Copy link
Author

v19.4.0 have #45812 , so should be fixed by now in latest version (though I haven't tested it personally)

@jaslong
Copy link

jaslong commented Jan 18, 2023

Note this bug made it into v18.13.0 as well. Related comment

@S1lander
Copy link

S1lander commented Mar 5, 2023

I'm still getting this bug in v18.14.2.

@KhafraDev
Copy link
Member

please post a new issue in https://github.com/nodejs/undici/ with a small repro

@straube
Copy link

straube commented Mar 8, 2023

Downgrading to an older Node version (applicable to my case) fixes the issue.

CGQAQ added a commit to prompt-engineering/click-prompt that referenced this issue Mar 9, 2023
CGQAQ added a commit to prompt-engineering/click-prompt that referenced this issue Mar 9, 2023
CGQAQ added a commit to prompt-engineering/click-prompt that referenced this issue Mar 9, 2023
teobler pushed a commit to prompt-engineering/click-prompt that referenced this issue Mar 9, 2023
teobler pushed a commit to prompt-engineering/click-prompt that referenced this issue Mar 9, 2023
teobler pushed a commit to prompt-engineering/click-prompt that referenced this issue Mar 9, 2023
@brendonco
Copy link

this bug made it to node v18.16.0

@Vrq
Copy link

Vrq commented Apr 24, 2023

@fawazahmed0 is there a fix for node 18.x?

@KhafraDev
Copy link
Member

I'd recommend using the fetch that undici exports, rather than the global one. Then you won't have to worry about old versions of node not receiving fixes or new features.

@Vrq
Copy link

Vrq commented Apr 24, 2023

@KhafraDev so you are basically saying that using the global fetch in node should be deprecated and the fetch itself should be removed?

@KhafraDev
Copy link
Member

No, not at all. I'm saying that if there's an issue, for example, in v19.0.0 then you either have to update node to a version that ships undici's fix(es), or use undici itself.

@Vrq
Copy link

Vrq commented Apr 24, 2023

The issue exists in v18.x versions as well (as other commenters pointed out)
Since Node 18 is still the actively supported version: https://nodejs.dev/en/about/releases till October 2023, one could expect that bug in the public API / feature would be fixed.

Otherwise, if that's not the case, this feature should not be added to the Node itself.

@KhafraDev
Copy link
Member

Fetch is currently marked as experimental which means that it is "not recommended in production environments."1

However, I don't see anything that would prevent someone from backporting one of the undici updates to v18.x, it's just that no one has. Of course then you'll still have issues in older node 18 versions. Undici's fetch works from v16.8.0 to v20 so you don't need to deal with backports, buggy versions, and the like.

Footnotes

  1. https://nodejs.org/dist/latest-v20.x/docs/api/documentation.html#stability-index

@alextoul
Copy link

I just migrated node from v18.x to v21.7.0 hoping to solve this issue but I still get this error in production.

node:internal/deps/undici/undici:13737
       Error.captureStackTrace(err, this);
            ^
TypeError: fetch failed
    at node:internal/deps/undici/undici:13737:13
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  [cause]: ConnectTimeoutError: Connect Timeout Error
      at onConnectTimeout (node:internal/deps/undici/undici:8285:28)
      at node:internal/deps/undici/undici:8237:50
      at Immediate._onImmediate (node:internal/deps/undici/undici:8269:13)
      at process.processImmediate (node:internal/timers:478:21) {
    code: 'UND_ERR_CONNECT_TIMEOUT'
  }
}
Node.js v21.7.0

My API call is entirely wrapped inside a try-catch. Should I stop using fetch?

selectionApiRouter.get('/selections/default/view', async (req: any, res) => {
  try {
    const currentMember: any = await sessionHelper.identifyMemberWithCookie(req, res)
    const calendarData = await calendarBuilder.buildCalendarViewData(currentMember)    // (fetch inside)
    res.send(calendarData)
  } catch (error) {
    log.critical('API GET /selections/:slug/view', error)
    res.status(500).send({ error: 'Internal server error' })
  }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fetch Issues and PRs related to the Fetch API
Projects
None yet
Development

No branches or pull requests

9 participants