node-fetch: Error: only absolute urls are supported
This happens when you are trying to get a relative path is ../../my/path/to/myfile.json. This is supported in browsers (tested on Firefox, Chrome and Safari).
My issue is in testing, tests fail because of the above error, which is annoying because now I have a distinction between the browser and the node environment running the tests.
This is the stack trace beyond my code:
at node_modules/node-fetch/index.js:54:10
at new Fetch (node_modules/node-fetch/index.js:49:9)
at Fetch (node_modules/node-fetch/index.js:37:10)
at Object.<anonymous>.module.exports (node_modules/isomorphic-fetch/fetch-npm-node.js:8:19)
I have added a comment in isomorphic-fetch discussion: https://github.com/matthew-andrews/isomorphic-fetch/issues/76#issuecomment-402784514, but would like to attract attention on this use case here too.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 24
- Comments: 17
Would be nice if the error message saying
only absolute urls are supportedincluded the actual URL and consequentially spared a bit of headache for the usersmaybe use
fetch('http://localhost:3000',...)instead offetch('/someEndpoint', ...)Agreed with @jimmywarting. You may want to use
new URL(relativeURL, baseURL)to resolve the URL before passing it to node-fetch.@kopax I was having the same issue, it seems to be some issues with the types files. I fixed my issue which was that I was using it like:
instead of
The server don’t have an origin so the default domain name is unknown. Therefore relative url won’t work. Hence the error.
Supporting file:// protocol is also out of the question.
I would like to close this issue as relative to https://github.com/bitinn/node-fetch/issues/75
How would you fix this in nextjs when using getStaticPaths() and getStaticProps() ?
Refer: https://stackoverflow.com/a/62512796/1929115
https://codeconqueror.com/blog/get-the-current-url-in-next-js
@dork1
It looks like there’s something wrong with the URL in the fetch method. For me, it solved by changing url in the fetch method. Pay attention to the correctness of the URL address.