Skip to content

Node.js 18: built-in fetch and the node:test runner

my first blog post

Node.js 18 added two features that remove the need for common npm packages: a global fetch() and a built-in test runner.

fetch first appeared in Node.js 17.5 behind the --experimental-fetch flag. In Node.js 18 it is available globally without a flag (along with Headers, Request, Response and FormData, all implemented by the undici project), so you no longer need node-fetch for simple HTTP requests. In 18 it was still marked experimental and printed an ExperimentalWarning on first use; it became stable in Node.js 21.

const getPokemon = async () => {
const res = await fetch('https://pokeapi.co/api/v2/pokemon/');
if (!res.ok) {
throw new Error(`Request failed with status ${res.status}`);
}
const data = await res.json();
console.log(data);
};
getPokemon().catch(console.error);

The API matches the browser fetch: it returns a Response with ok, status, json(), text() and so on. Note that fetch only rejects on network errors, not on HTTP error statuses like 404, so check res.ok yourself.

Node.js 18.0 also added the node:test module, so you can write tests without installing Jest or Mocha. It must be imported with the node: prefix.

math.test.mjs
import test from 'node:test';
import assert from 'node:assert';
test('synchronous passing test', () => {
// Passes because it does not throw.
assert.strictEqual(1, 1);
});

Run a single file with node math.test.mjs, or run every test file Node can find with node --test (the --test flag was added in Node.js 18.1). The .mjs extension is needed for the import syntax unless your package.json sets "type": "module".

In Node.js 18 the runner was experimental. It became stable in Node.js 20, which also added more features, so check the node:test docs for the version you run.

© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)