Beginning require use with an NPM package
suggest changeModern Node.js includes the web-standard fetch() API (global fetch since Node.js 18), so a simple HTTP request does not require an npm package:
async function getWeather(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
return response.json();
}
getWeather('https://example.com/weather')
.then(console.log)
.catch(console.error);
fetch() resolves to a Response; check response.ok before decoding the response body.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents