Node.js 20 is here

Published on

Node.js 20 has been released, and it brings several new features and improvements for developers.

Let’s take a closer look at some of them.

Stable test runner

Node.js 20 introduces a stable test runner module!

You can use describe, it/test, and hooks to structure test files, as well as watch mode, running multiple test files in parallel, and mocking.

import { test, mock } from 'node:test';
import assert from 'node:assert';
import fs from 'node:fs';

mock.method(fs, 'readFile', async () => "Hello World");

test('reads file content', async (t) => {
  assert.strictEqual(await fs.readFile('a.txt'), "Hello World");
});

Note that some features of the test runner are not yet stable.

Permission Model

The experimental Permission Model allows developers to restrict access to specific resources during execution.

For example, you can manage file system access, child_process, worker_threads, and native addons.

Use the --experimental-permission flag.

You can also use the --allow-* flags to grant specific permissions.

Use specific paths for file system access by passing comma-separated values to the flags.

For example, this command allows write access to the /tmp/ folder:

$ node --experimental-permission --allow-fs-write=/tmp/ --allow-fs-read=/home/index.js index.js

Use process.permission.has() method to check if a certain permission has been granted at runtime. For example:

if (process.permission.has('fs.write')) {
  // ...
}

if (process.permission.has('fs.write', '/etc/hosts')) {
  // ...
}

It’s still experimental and may change in future releases of Node.js.

Performance improvements

E.g., the cost of initializing EventTarget has been cut by half, while V8 Fast API calls have been leveraged to improve performance in APIs like URL.canParse() and timers.

EOL for Node.js 14

Node.js version 14 is set to reach End-of-Life in April 2023. Therefore, upgrading to either Node.js 18 (LTS) or Node.js 20 (soon to be LTS) is recommended to ensure that applications remain secure and up to date.

View the full changelog for more details:

https://nodejs.org/en/blog/announcements/v20-release-announce

Here, have a slice of pizza 🍕