Node.js 18 released, introducing global Fetch API and core test runner module

Node.js 18 has been released , and highlights of this release include updating the V8 JavaScript engine to 10.1, enabling the global Fetch API by default, and the core test runner module. Node.js 18 is the "current" release for the next 6 months, and then upgrades to LTS in October 2022, after which it will be supported until April 2025.

New browser compatible API

Global Fetch API (experimental)

Node.js 18 provides an experimental global Fetch API by default, implemented from the HTTP/1.1 client undici and inspired by node-fetch .

Example usage of this API:

const res = await fetch('https://nodejs.org/api/documentation.json');
if (res.ok) {
  const data = await res.json();
  console.log(data);
}

With this addition, the following global variables are available: fetch, FormData, Headers, Request, Response.

  • The API can be disabled via --no-experimental-fetchcommand line flags.
  • The API will remain experimental until more test coverage is added and the API implements as many specifications as possible.

Web Streams API (experimental)

Node.js now exposes an experimental implementation of the Web Streams API globally . This means the following APIs are available:

  • ReadableStream, ReadableStreamDefaultReader, ReadableStreamBYOBReader, ReadableStreamBYOBRequest, ReadableByteStreamController, ReadableStreamDefaultController, TransformStream, TransformStreamDefaultController, WritableStream, WritableStreamDefaultWriter, WritableStreamDefaultController, ByteLengthQueuingStrategy, CountQueuingStrategy, TextEncoderStream, TextDecoderStream, CompressionStream, DecompressionStream.

Other global APIs

Additionally, the following APIs are now exposed globally:

test runner module (experimental)

Introduces a new node:testmodule to help create JavaScript tests that report results in TAP format. This module can be import test from 'node:test';imported . Here is an example of a parent test with two child tests:

test('top level test', async (t) => {
  await t.test('subtest 1', (t) => {
    assert.strictEqual(1, 1);
  });

  await t.test('subtest 2', (t) => {
    assert.strictEqual(2, 2);
  });
});

Note: Test runner modules can only use node:prefixes, the node:prefix means to load the core module, omitting the prefix and importing 'test'will try to load the userland module.

Read more about this test runner in the documentation.

Toolchain and compiler upgrades

Node.js provides pre-built binaries for several different platforms.

  • Prebuilt binaries for Linux are now built on Red Hat Enterprise Linux (RHEL) 8 and are compatible with Linux distributions based on glibc 2.28 or higher, such as Debian 10, RHEL 8, Ubuntu 20.04.
  • Prebuilt binaries for macOS require macOS 10.15 or later.
  • For AIX, the minimum supported architecture was raised from Power 7 to Power 8.
  • Due to issues building V8 dependencies in Node.js, pre-built binaries for 32-bit Windows will not be available.

V8 updated to 10.1

The V8 engine is updated to version 10.1, which includes the following new features:

Update Announcement | Complete Update List | Download Address

Guess you like

Origin www.oschina.net/news/192150/nodejs-18-released