Node.js 21 is now available!

By admin
Node.js

21
CARDINAL

is now available!

We’re excited to announce the release of

Node.js 21
LAW

! Highlights include updates of the

V8
ORG


JavaScript
PRODUCT

engine to

11.8
CARDINAL

, stable fetch and

WebStreams
ORG

, a new experimental flag to flip module defaults ( –experimental-default-type ), a built-in WebSocket client, many updates to our test runner, and more!

Node.js

21
CARDINAL

will replace Node.js

20
CARDINAL

as our ‘Current’ release line when Node.js

20
CARDINAL

enters long-term support (

LTS
ORG

)

later this month
DATE

. As per the release schedule, Node.js

21
CARDINAL

will be ‘Current’ release for

the next 6 months
DATE

, until

April 2024
DATE

.

The project continues to make progress across a number of areas, with many new features and fixes flowing into existing

LTS
ORG

releases. For that reason, the changes outlined in the changelog for

Node.js 21
LAW

only represent a small subset of the features and work since the last major release. This blog post will add some additional context on the larger body of work in relation to those changes.

You can read more about our release policy at

https://github.com/nodejs/release
WORK_OF_ART

.

To download Node.js

21.0.0
CARDINAL

, visit: </download/current/>. You can find the release post at </blog/release/

v21.0.0
ORG

>, which contains the full list of commits included in this release.

Notable Changes

Stable fetch/WebStreams

The recent update to Node.js, version

21
CARDINAL

, includes an important change to the fetch module as well as

WebStreams
ORG

. Both modules were marked as stable after a recent update.

This impacts

WebStreams
ORG

,

FormData
ORG

, Headers , Request , Response , and fetch .

Contributed by

Steven
PERSON

in #

45684
MONEY

.

Built-in WebSocket client

A experimental browser-compatible WebSocket implementation arises with this release. This is enabled through the flag: –experimental-websocket . As any experimental feature, that’s subject to change.

Contributed by Khafra in #

49830
MONEY

.


V8 11.8
PRODUCT

As per usual a new version of the

V8
PRODUCT

engine is included in Node.js (updated to version

11.8
CARDINAL

, which is part of

Chromium 118
ORG

) bringing improved performance and new language features including:

The

V8
PRODUCT

update was a contribution by

Michaël Zasso
PERSON

in #

47251
MONEY

.

Support for globs in the Node.js test runner

With the latest Node.js update, the test runner introduces support for glob expressions when specifying the –test parameter. This means you can now use powerful glob patterns to run tests more efficiently and flexibly. For example, you can execute tests for all files with the .test.js extension across multiple directories using a command like node –test **/*.test.js .

Contributed by

Moshe Atlow
PERSON

in #

47653
MONEY

.


ESM
ORG

: –experimental-default-type flag to flip module defaults

The new flag –experimental-default-type can be used to flip the default module system used by Node.js. Input that is already explicitly defined as

ES
ORG

modules or CommonJS, such as by a package.json "type" field or .mjs / .cjs file extension or the –input-type flag, is unaffected. What is currently implicitly CommonJS would instead be interpreted as

ES
ORG

modules under –experimental-default-type=module :

String input provided via –eval or

STDIN
ORG

, if –input-type is unspecified.

or

STDIN
ORG

, if is unspecified. Files ending in .js or with no extension, if there is no package.json file present in the same folder or any parent folder.

or with no extension, if there is no file present in the same folder or any parent folder. Files ending in .js or with no extension, if the nearest parent package.json field lacks a type field; unless the folder is inside a node_modules folder.

In addition,

extensionless
PERSON

files are interpreted as

WebAssembly
ORG

if –experimental-wasm-modules is passed and the file begins with the

WebAssembly
ORG

preamble \0asm .

We are also exploring using detection of

ES
PRODUCT

module syntax as a way of Node.js knowing when to interpret files as

ES
ORG

modules. Our goal is to eventually find a way to support

ES
ORG

module syntax by default with minimal breaking changes.

Contributed by

Geoffrey Booth
PERSON

in #

49869
MONEY

.

Module customization hook globalPreload removed; use register and initialize instead

The module customization hook globalPreload has been removed. Instead, use register to send data from the application thread to the customization hooks, and the initialize hook to establish a communications channel between the threads.

Contributed by

Jacob Smith
PERSON

in #

49144
MONEY

.

Add flush option to fs.writeFile function

When writing to files, it is possible that data is not immediately flushed to permanent storage. This allows subsequent read operations to see stale data. This PR adds a ‘flush’ option to the fs.writeFile family of functions which forces the data to be flushed at the end of a successful write operation.

Contributed by

Colin Ihrig
PERSON

in #

50009
MONEY

.


Performance

Performance
PRODUCT

is an important attribute of a runtime and our @nodejs/performance team has been working hard over

the last year
DATE

to make improvements in URL, fetch, streams, node:fs and HTTP.

Streams

The Node.js streams team keeps on optimizing

Writable
FAC

and Readable streams. In this version, streams maintainer

Robert Nagy
PERSON

led an effort to further optimize streams by removing redundant checks, utilizing bitmaps, and scheduling callbacks in a more efficient way.

Contributed in #

50012
MONEY

.

HTTP

Previously, when writing to a chunked response, Node.js would create a separate chunk for each call to .write(…) regardless of whether the response was corked or not. This leads to unnecessary overhead both on the client and server side.

This change fixes this by creating a single chunk for all calls to write(…) when uncorking the response.

Consider the following example based on

the Transfer-Encoding
LAW

docs:

res . cork () ; res . write ( ‘

Mozilla
ORG

‘ ) ; res . write ( ‘ Developer Network ‘ ) ; res . uncork () ;

At the beginning of each chunk you need to add the length of the current chunk in hexadecimal format, followed by ‘\r

‘ and then the chunk itself, followed by another ‘\r

‘. The terminating chunk is a regular chunk, with the exception that its length is

zero
CARDINAL

.

Resulting in a response stream:

HTTP/1.1

200
CARDINAL

OK Content-Type: text/plain Transfer-Encoding: chunked 7\r

Mozilla\r


18\r
DATE

Developer Network\r


0\r
CARDINAL

\r

After this PR everything is combined into a single chunk when uncorking the response bypassing a lot of unnecessary overhead.

HTTP/1.1

200
CARDINAL

OK Content-Type: text/plain Transfer-Encoding: chunked

25\r
CARDINAL


Mozilla Developer
ORG

Network\r


0\r
CARDINAL

\r

Contributed by

Robert Nagy
PERSON

in #

50167
MONEY

.

llhttp

9.1.2
CARDINAL

strict mode enforcement

In previous versions of Node.js, strict mode was not enabled by default. With the latest update, all settings that were previously included in strict mode are enabled by default, enhancing code reliability and security.

The presence of \r

after headers is now mandatory (previously, \r alone was allowed). Additionally, \r

after a chunk is now a requirement, ensuring consistent data processing.

after headers is now mandatory (previously, alone was allowed). Additionally, after a chunk is now a requirement, ensuring consistent data processing. Data transmission after a message with the Connection: close header has been parsed is no longer allowed. This change enhances protocol adherence and improves connection handling.

To accommodate specific use cases, the –insecure-http-parser flag exists. This option allows users to disable the aforementioned changes and maintain backward compatibility with previous parsing behavior.

These updates are designed to enhance overall system stability and improve the consistency of data processing in Node.js applications. Developers are encouraged to review their codebase and adjust their implementations accordingly to ensure seamless integration with the latest version.

navigator Object integration

In Node.js

21
CARDINAL

, we’ve introduced the global navigator object, enhancing web interoperability. Now, developers can access hardware concurrency information through navigator.hardwareConcurrency

Contributed by

Yagiz Nizipli
ORG

in #

47769
MONEY

.

Deprecations

[

4b08c4c047
CARDINAL

] – (

SEMVER
ORG

-MAJOR) lib : runtime deprecate

punycode
ORG

(

Yagiz Nizipli
ORG

) #

47202
MONEY

] – : runtime deprecate

punycode
ORG

(

Yagiz Nizipli
ORG

) #

47202
MONEY

[ ccca547e28 ] – (

SEMVER
ORG

-MAJOR) util: runtime deprecate promisify -ing a function returning a

Promise
PERSON

(

Antoine du Hamel
ORG

) #49609

Call to action!

Try out the new Node.js

21
CARDINAL

release! We’re always happy to hear your feedback. Testing your applications and modules with

Node.js 21
LAW

helps to ensure the future compatibility of your project with the latest Node.js changes and features.

Also of note is that Node.js

16
CARDINAL

(

LTS
ORG

) is End-of-Life, so we strongly advise you to start planning to upgrade to Node.js

18
CARDINAL

(

LTS
ORG

) or Node.js

20
CARDINAL

(

LTS
ORG

).