Fetch json API in Node.js v18

Viewed 128

I am trying to fetch API from an URL in JavaScript and this is my code:

    try {
        let response = await fetch(URL, {
            method: 'GET',
        });
        let data = await response.json();
        console.log(data);
    }
    catch(err) {
        console.log("Err")
    }

Error I got:

(node:403113) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
node:internal/deps/undici/undici:4045
            if (socket[kParser].timeoutType !== TIMEOUT_IDLE) {
                                ^
TypeError: Cannot read properties of undefined (reading 'timeoutType')
    at _resume (node:internal/deps/undici/undici:4045:33)
    at resume (node:internal/deps/undici/undici:4015:7)
    at connect (node:internal/deps/undici/undici:4004:7)
Node.js v18.6.0

I googled and what I know is I should run it on Node.js v16 :((

1 Answers

[SOLVED] I got the answer on a Indian guy Youtube channel. Instead of using fetch, I use axios.get for getting the data from Json API. Thanks for you all supporting <3 So it becomes:

let URL ="......."       
try {
    let response = await axios.get(URL);
    let to_return = await response.data;
    console.log(to_return);
}
catch(err) {
    console.log("Err")
}
Related