In April 2022, NodeJS release Node v18 was issued. The Node v18 update has been one of the most anticipated updates since Node v14, which added async local storage API, a Web Assembly System Interface (WASI), and support for ECMAScript Modules.

The v18 upgrade adds various improvements to NodeJS, including a globally enabled fetch API by default, web stream API support, and a core test runner module. Here we cover most of the significant changes to NodeJS in v18.

1. The Fetch API

The fetch API provides an interface for accessing and manipulating HTTP requests and responses. Before NodeJS v18, HTTP requests made with NodeJS required third-party libraries like axios and node-fetch or the complicated http or https package.

NodeJS v18 makes the fetch API available in the global scope by default. The global fetch API eliminates the need for third-party libraries like axios and the verbose code associated with the https package when fetching resources asynchronously across a network.

Here’s an example of the usage of the API:

        const getData = async function () {
  try {
    const res = await fetch("https://example.com/users");
    const data = await res.json();
    console.log(data);
  } catch (error) {
    console.log(`Something went wrong, ${error}`);
  }
};
getData();

The getData function above sends a GET request to fetch the resources from "https://example.com/users". The fetch API also takes an optional second parameter, an options object. The options objects should contain details about the request. For example:

        const userData = {
  name: "Jon Snow",
  age: 25,
  gender: "M",
};

const postData = async function (data) {
  try {
    const res = await fetch("https://example.com/users", {
      method: "POST",
      body: JSON.stringify(data),
    });
    const responseData = await res.json();
    console.log(
      `The request was sucessful, Here are the details: ${responseData}`
    );
  } catch (error) {
    console.log(`The request was unsucessful, Here are the details: ${error}`);
  }
};
postData(userData);

Although the fetch API is still experimental, it should work well for most users.

2. Support for Web Streams API

Think multimedia or real-time applications; this is a core use case of the web-streams API. The web streams API is a set of interfaces that enable the programmatic breakdown of a large resource received over a network into small chunks, then processes it bit by bit.

An example of a stream is the response body returned by a successful fetch request. The response body is of the type ReadableStream.

NodeJS v18 exposes an experimental implementation of the web streams API. With this update, web stream APIs are now available in both the client and server, making development more straightforward.

You can find a list of the currently supported streams in the official release notes.

3. The Test Runner Module

In earlier versions of NodeJS, testing without third-party libraries was done with the built-in assert library.

The assert library made it easy to write simple tests, but it had significant limitations that made it hard to work with multiple test suites. These limitations fostered the need for third-party libraries like Jest to run more advanced tests.

NodeJS v18 eliminates the need for third-party libraries with the introduction of NodeJS's own core test runner module. The test runner supports features like subtests, test skipping, callback tests, etc., much like Jest.

To use the test runner, you must import assert from assert and test from node:test. Notice the "node:" prefix; it is Node's new protocol that distinguishes node core packages from user-created packages.

        const assert = require("assert");
const test = require("node:test");

Here’s an example of a simple test using the test runner module:

        test("Check if strings are the same", (t) => {
  assert.strictEqual("test", "test");
});

The test results displayed by the test runner will be in TAP (Test Anything Protocol) format. Note that this feature is still experimental and might change at any time.

4. Other Upgrades and Fixes

The Node v18 update also features an update to the v8 engine to version 10.1. The v8 engine is the JavaScript runtime used by NodeJS to execute JavaScript.

The v8 update makes the array methods findLast and findLastIndex available in NodeJS and improves the performance of class fields and private class methods.

NodeJS also exposes the following APIs on the global scope in the Node v18 upgrade:

Upgrading to Node v18

Using Node v18, you can test an entire application without external libraries, fetch resources asynchronously without external libraries, and have web streams enabled in your server.

However, it is worth noting that these features are still experimental and can change anytime. You can upgrade to NodeJS v18 using the official download link.