Skip to content

Fetch doesn't work in 19.3.0 #46049

Description

@fawazahmed0

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()

Activity

  1. KhafraDev commented on Jan 2, 2023

    @KhafraDev
    Member

    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.

  2. fawazahmed0 commented on Jan 10, 2023

    @fawazahmed0
    Author

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

  3. jaslong commented on Jan 18, 2023

    @jaslong

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

  4. S1lander commented on Mar 5, 2023

    @S1lander

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

  5. KhafraDev commented on Mar 5, 2023

    @KhafraDev
    Member

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

  6. straube commented on Mar 8, 2023

    @straube

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

  7. brendonco commented on Apr 16, 2023

    @brendonco

    this bug made it to node v18.16.0

  8. Vrq commented on Apr 24, 2023

    @Vrq

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

  9. KhafraDev commented on Apr 24, 2023

    @KhafraDev
    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.

  10. Vrq commented on Apr 24, 2023

    @Vrq

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

  11. KhafraDev commented on Apr 24, 2023

    @KhafraDev
    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.

  12. Vrq commented on Apr 24, 2023

    @Vrq

    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.

  13. KhafraDev commented on Apr 24, 2023

    @KhafraDev
    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 ↩

  14. alextoul commented on Mar 11, 2024

    @alextoul

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    fetchIssues and PRs related to the Fetch API.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions