During a test in my hardhat project on VSCode, I encountered the need to retrieve the metadata object of my NFT from a specified URL.
Initially, I assumed I would have to import fs to read the URL. However, while typing out the method, I impulsively opted to utilize a method called fetch
with the intention of using a package like angular's 'HTTP' for fetching.
To my surprise, no error occurred when using the keyword 'fetch', indicating that it was defined somewhere.
Upon investigating further by right-clicking, I was directed to a file named lib.dom.ts
, which seemed to be part of the typescript node module.
Unfortunately, despite compiling successfully, the functionality did not work as intended.
When the function containing these lines is executed:
let uri = await nft.tokenURI(tokenId);
let response = await fetch(uri);
let json = await response.json();
An unexpected runtime error occurs during the hardhat test but without any compile errors -
ReferenceError: fetch is not defined
Is there a proper way to use fetch
in a typescript project outside of a browser environment without the need to import an HTTP package?