Node.js 21 is here with Websocket

RisingStack's services:


Node.js Experts

Learn more at

Sign up to our newsletter!

In this article:

The latest major version of Node.js has just released with a few new interesting experimental features and a lot of fixes and optimization. You can find our highlights from the release notes.

Built-in WebSocket client

A browser-compatible WebSocket implementation has been added to Node.js with this new release as an experimental feature. You can give it a go using the --experimental-websocket flag. The current implementation allows for opening and closing of websocket connections and sending data. There are four events available for use: open, close, message and error – so the basics are covered. It’s pretty exciting to see an out-of-the-box websocket implementation coming to Node, it could spare us the inclusion of yet another library in projects that need bidirectional communication. Be sure to give it a go and give your feedback to the developers!

A flush option for the writeFile type filesystem functions

Up until now, it was possible for data to not be flushed immediately to permanent storage when a write operation completed successfully, allowing read operations to get stale data. In response, a flush option has been added to the fs module file writing functions that, when enabled, forces data to be flushed at the end of a successful write operation using sync. This feature is not enabled by default, so make sure to include { flush: true } in the options if you’d like to use it.

Here is the list of functions the flush option has been added to:

  • filehandle.createWriteStream
  • fsPromises.writeFile
  • fs.createWriteStream
  • fs.writeFile
  • fs.writeFileSync

Addition of a global navigator Object

This new release also introduces a global navigator object to take steps towards enhancing web interoperability. We can now access hardware concurrency information through navigator.hardwareConcurrency, the only currently implemented method on the object. While this might not seem like a huge change for now, we can assume more and more functionality will be implemented with time, until we have the whole suit of information window.navigator provides in browser environments. This would spare us having to decide between using process and navigator in our code that is to be ran in both a browser and in Node.js.

Array grouping

There is a new static method added to Object and MapgroupBy(), that groups the items of a given iterable according to a provided callback function. The object returned contains a property for each group, whose value is an array with the items that belong to the group. In case of Object, the keys of the returned object will be strings, while the version on Map can have any kind of key.

Additional changes

  • Both the fetch and the webstreams modules are now marked as stable after receiving a few changes with this version.
  • A host of performance improvements as usual with any new release.
  • WebAssembly gets extended const expressions
  • Another new experimental flag, --experimental-default-type, has been added that allows setting the default module type to ESM
  • The globalPreload hook has been removed, it’s functionality replaced by register and initialize
  • Glob patterns are now supported in the test runner

Don’t forget that Node.js 16 is at its end of life, so if you’re still using this version, you should plan to upgrade soon to one of the newer LTS versions as soon as possible! The currently active LTS releases are 18 and 20, with version 22 – that is also an LTS version – scheduled to release in April 2024. You can find more information about the release schedule here.

Share this post



Learn more at

Node.js Experts