Repository navigation
Fetch doesn't work in 19.3.0 #46049
Copy link
Copy link
Closed
Labels
fetchIssues and PRs related to the Fetch API.Issues and PRs related to the Fetch API.
Description
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()Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
fetchIssues and PRs related to the Fetch API.Issues and PRs related to the Fetch API.
Activity
KhafraDev commentedon 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.
fawazahmed0 commentedon Jan 10, 2023
v19.4.0 have #45812 , so should be fixed by now in latest version (though I haven't tested it personally)
jaslong commentedon Jan 18, 2023
Note this bug made it into v18.13.0 as well. Related comment
S1lander commentedon Mar 5, 2023
I'm still getting this bug in v18.14.2.
KhafraDev commentedon Mar 5, 2023
please post a new issue in https://github.com/nodejs/undici/ with a small repro
straube commentedon Mar 8, 2023
Downgrading to an older Node version (applicable to my case) fixes the issue.
fix: restrict nodejs version (nodejs/node#46049)
fix: restrict nodejs version (nodejs/node#46049)
fix: restrict nodejs version (nodejs/node#46049)
fix: restrict nodejs version (nodejs/node#46049)
fix: restrict nodejs version (nodejs/node#46049)
fix: restrict nodejs version (nodejs/node#46049)
brendonco commentedon Apr 16, 2023
this bug made it to node v18.16.0
Vrq commentedon Apr 24, 2023
@fawazahmed0 is there a fix for node 18.x?
KhafraDev commentedon Apr 24, 2023
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 commentedon 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 commentedon Apr 24, 2023
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 commentedon 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 commentedon Apr 24, 2023
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
https://nodejs.org/dist/latest-v20.x/docs/api/documentation.html#stability-index ↩
alextoul commentedon Mar 11, 2024
I just migrated node from v18.x to v21.7.0 hoping to solve this issue but I still get this error in production.
My API call is entirely wrapped inside a try-catch. Should I stop using fetch?