In a test in my hardhat project I am writing in VSCode, I want to retrieve the metadata object of my NFT, which is stored at a provided URL.
I thought I would have to import fs to read the URL, but I was typing the method, I arbitrarily decided to use a method called fetch
for the purpose of using some package like angular's 'HTTP' to perform the fetch.
But I was surprised that no error was reported on the keyword 'fetch' it appears the fetch keyword was defined somehow.
Wondering where it came from a right click took me to a file called lib.dom.ts
, apparently part of the typescript node module.
The thing is, it doesn't actually work, even though it compiles.
When my function, which includes these lines,
let uri = await nft.tokenURI(tokenId);
let response = await fetch(uri);
let json = await response.json();
is run using hardhat test, I don't get a compile error, but instead a runtime error -
ReferenceError: fetch is not defined
Is there a way to properly use fetch
in a typescript project that is not running in a browser without having to import some HTTP package?
CodePudding user response:
You are likely to use an outdated version of node.js
fetch
is only available from v17.5 onwards.