snow-mountain

Node.js 18 — A Deeper Look Into the Newest Version and Its Features


Last updated on
Web Development

Summary (TL;DR): For the JavaScript community, Node.js comes with so much excitement and more compatible browser APIs that aim to make for more seamless development with Node.js, such as native features like fetch and webstream APIs. Node.js 18 will be the current release of Node.js, and in this blog post, we will look at everything you need to know about Node.js 18!

Node.js is an open-source runtime environment for creating server-side applications with JavaScript. It’s a fast runtime, having been built on Chrome’s V8 engine and its open-source, providing support from the community. 

For most teams and software enthusiasts, Node.js delivers essential features like asynchronous functions, and with the new version of Node.js, new features like Fetch and web stream APIs.
In the next section, we will look at the latest features available on Node.js 18.

While You Are at It, Why Not Learn More About The Newest Version of React – React 18

Table of Contents

Latest Features in Node.js 18
>> New and Globally Available Browser-Compatible APIs in Node.js 18
>> Experimental Test Runner Module
>> ToolChain Upgrades
>> V8 engine upgraded to 10.1
>> Build Time User-Land Snapshot
>> HTTP Timeouts
Why You Should Upgrade Your Existing Project From Node 17 to 18
>> Migration

Latest Features in Node.js 18

New and Globally Available Browser-Compatible APIs in Node.js 18

Before Node.js 18, developers had to use the http.request API or some third-party packages like Axios. The http.request API requires you to write a bunch of code when making a request, while the pain of installing different packages for fetching data is well known to many Node.js developers. Below is an example of fetching with the request API:

const request = require('request');

request('http://www.google.com', function (error, response, body) {
  console.error('error:', error); // Print the error if one occurred
  console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
  console.log('body:', body); // Print the HTML for the Google homepage.
});

You first had to call the request function, open a URL, and send the request. After that, you can then log the fetched data to the console.

With the new support for Fetch API, you can make an HTTP request as easy as possible.

const response = await fetch('fetch something from a url');
const data = await response.json();
console.log(data);

With the fetch API, our code for fetching and logging to the console becomes noticeably smaller.

Note: It is important to note that although the Fetch API is available by default in Node.js 18, it is experimental at this point and will be so until more test coverage is verified.

Another browser-compatible API coming to Node.js 18 is Streams or Web Streams. One reason for the popularity of Node.js since its introduction is that it opened a way of writing JavaScript for the server-side. One available API in JavaScript was the Streams API. Node.js is now cementing its place in developers’ hearts by introducing the Streams API in Node.js 18.

The Streams API allows Node.js and JavaScript to access data streams programmatically across the network and then process them as the developer desires. Streaming involves breaking down data or resources (audio, video, or text files) that you want to send, transform or receive into small chunks and then processing those chunks bit by bit.

Before now, you will have to download the file you want, wait for your file to be deserialized into a suitable format, and then process. This process generally makes sites, especially songs and video sites, feel slow and laggy.

Below are some classes now available for the Streams API:

  • ReadableStream: This represents a source of streaming data. Below are some methods available for the ReadableStream class:
    • ReadableStreamDefaultReader
    • ReadableStreamBYOBReader
    • ReadableStreamBYOBRequest
    • ReadableByteStreamController
    • ReadableStreamDefaultController
  • TransformStream: This represents an algorithm for transforming streaming data. Below are some methods available for the TransformStream class:
    • New TransformStream
    • TransformStream.readable
    • TransformStream.writable
    • Transferring with postMessage()
    • TransformStreamDefaultController
  • WritableStream: This represents a destination for streaming data. Below are some methods available for the WritableStream class:
    • WritableStreamDefaultWriter
    • WritableStreamDefaultController
  • CompressionStream: This compresses a stream of data using a zip or deflated format. Below are some methods available for the CompressionStream class:
    • new CompressionStream(format)
    • compressionStream.readable
    • compressionStream.writable
  • DecompressionStream: This decompresses the data stream using the gzip or deflate format. Below are some methods available for the DecompressionStream class:
    • new DecompressionStream(format)
    • DecompressionStream.readable
    • DecompressionStream.writable

You can check the official Node.js docs here to learn more about Streams in Node.js.

Experimental Test Runner Module

The new version of Node now comes with out-of-the-box testing for node projects, thereby allowing developers to write tests for Node.js projects without third-party libraries or packages.

To use the experimental test runner module, you will have to import the test from Node:test. Node.js will try loading a third-party test module without the node prefix, Node.js will try loading a third-party test module.

import test from 'node:test';
import assert from 'node:assert';

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);
  });
});

ToolChain Upgrades

Toolchains are a set of software development tools used in combination with one another to complete or deliver a complex software product.Toolchains are always accessed and raised appropriately whenever there is a new release for Node.js. Below are the upgrades for Node.js 18:

  • 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 later, for example, Debian 10, RHEL 8, Ubuntu 20.04.
  • Prebuilt binaries for macOS now require macOS 10.15 or later.
  • For AIX, the minimum supported architecture has been raised from Power 7 to Power 8.

Prebuilt binaries for 32-bit Windows will initially be unavailable due to issues building the V8 dependency in Node.js. We hope to restore 32-bit Windows binaries for Node.js 18 with a future V8 update. You can learn in-depth information on the upgrades to Toolchains in Node.js 18.

V8 Engine Upgraded to 10.1

The V8 engine is the Javascript engine that powers Google Chrome and provides the environment for executing Javascript.

Node.js 18 comes with the upgraded V8 engine v10.1. This upgrade comes with various new features, which include:

  • The findLast() and findLastIndex() array methods. Developers can use this method to locate an element that satisfies a certain condition. The findLast() method takes a predicate and returns the first element in the array for which the predicate returns true. While the findLastIndex() array method works the same way, except that it returns the index when found and -1 when not found.
  • Improvements to the Intl.LocaleAPI.
  • The Intl.supportedValuesOf function.
  • Improved performance of class fields and private class methods, with the new changes, you can initialize the class fields and private classes as fast as you’d initialize ordinary property stores.

Build Time User-Land Snapshot

From Node.js 18, developers and users can now build a Node.js binary with experimental custom V8 snapshots. This can be done using the –node-snapshot-main flag of the configured script.

$ cd /path/to/node/source

# Specifying an entry point of the snapshot, for example,
# a UMD module like the marked markdown renderer, which in
# this case should initialize the renderer and store it in
# globalThis.
$ ./configure --node-snapshot-main=marked.js

# Build the binary
$ make node

HTTP Timeouts

Another noticeable change coming to Node.js 18 is the change to the HTTP.server timeout. The built-in module allows Node.js to transfer data over the Hyper Text Transfer Protocol. The headerTimeout which is the time allowed for an HTTP request header to be parsed is now set to 60000 milliseconds (60 seconds) by default instead of the previous 40000 milliseconds (40 seconds). 

The requestTimeout API is the timeout API used for the HTTP request and is now set to 300000 seconds ( 5 minutes ) as the default.

Why You Should Upgrade Your Existing Project From Node 17 to 18 (Migration and Deprecation)

There are lots of reasons why you should upgrade. Some of the reasons can be found below: 

  • Globally available browser APIs like Fetch and Web Streams API for developers
  • Version upgrades of the V8 engine come with new and advanced features like new array methods, improvements, and enhancements to the Intlq1.Locale API etc
  • Test runner module that allows you to write tests for your application without the need for third-party packages.
  • Deprecated APIs: One of the reasons you should upgrade your existing project to Node.js 18 is because the introduction of Node.js 18 brought some new changes and had some APIs deprecated. You don’t want to experience errors in your console due to some deprecated APIs when using Node.js. Here is a link to a complete list of deprecated APIs for Node.js 18.

Migration

For Linux and Mac users, follow these commands to upgrade to the latest version of Node:

The module n makes version management easy:

npm install n -g

For the latest stable version:

n stable

For the latest version:

n latest

Windows Users

Visit the Node.js docs here to download the new version of Node.js

Note: Note that the browser APIs and most features in Node.js 18 are experimental, meaning that they are still undergoing tests and may not work seamlessly.

Conclusion

In this article, we have taken an in-depth look at the latest version of Node.js, its breaking changes, and why you should upgrade your existing project to the newest version. We also looked at deprecated APIs and the update to the V8 engine.