NodeJS 22: Require() Support & WebSockets

NodeJS 22: Require() Support & WebSockets
Photo by Thomas Couillard / Unsplash

The newest version of NodeJS – the latest release of the most popular JavaScript Runtime – has officially arrived! The new release is featuring better require() support for ECMAScript modules. This is a huge improvement for the WebSocket client with an updated version of the Google v8 JavaScript Engine.

With it being announced today – April 24th, 2024 – version 22 is able to be downloaded at the NodeJS.org website. The new version has added  require() support for synchronous ECMAScript module graphs under the following flag when it is used.

--experimental-require-module

Here is a very simple code example.

// ES module: math.js
export function add(a, b) {
    return a + b;
}

// CommonJS module: app.js
const { add } = require('./math.js');
console.log(add(2, 3)); // Output: 5

When the flag is enabled and the ES module meets some requirements, the require() function will load the requested module accordingly. On top of this feature, NodeJS 22 also includes an experimental feature for the execution of scripts from package.json with the following command-line (CLI) flag.

node --run <script-in-package-json>

Then here is a very simple code example of how this will work.

// package.json
{
  "name": "myapp",
  "scripts": {
    "start": "node index.js"
  }
}

Then you will run the following for it to work from the command line.

node --run start

Google v8 JavaScript Engine Updated

The Google v8 JavaScript Engine has been updated to 12.4 with this newest release of NodeJS. This update includes features such as WebAssembly Garbage Collection and Iterator Helpers. The V8’s Maglev Optimizing Compiler now is enabled by default on some architectures. WebSocket communications have also been updated with a browser-compatible implementation enabled by default. We can now use a WebSocket Client to connect to NodeJS without requiring any external dependencies – where as in the past, you would be required to use a flag for the implementation.

Here is how to use WebAssembly with NodeJS.

// Using WebAssembly in Node.js
const fs = require('fs');
const buf = fs.readFileSync('module.wasm');
const wasmModule = new WebAssembly.Module(buf);
const wasmInstance = new WebAssembly.Instance(wasmModule);

Here is an example of WebSocket clients to connect to NodeJS without requiring any external dependencies.

// WebSocket Client in Node.js
const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost:8080');

ws.on('open', function open() {
  ws.send('hello from Node.js!');
});

ws.on('message', function incoming(data) {
  console.log(data);
});

Other NodeJS Updates

NodeJS also includes enhancements to developing a AbortSignal instance. This helps improves performance in fetchcode and test runners. The high water makr for streams was increased from 16KB to 64KB – this provides a good performance boost for the cost of needing slightly more memory to run an application.

Here is an very simple example of AbortSignael.

const controller = new AbortController();
const signal = controller.signal;
// Use signal in fetch or test runners
fetch('url', { signal });

From this version and going forward, watchmode is now officially considered stable. When you use watchmode, changes in the watch files cause the NodeJS process to restart when there is an error or crash. Then, also, for pattern matching, NodeJS 22 added to the node:fs module functions glob and globSync. Developers can now utilize these functions for matching file paths based on specific patterns that they want to match for the needs of their application.

Finally, here is a very simple code example for glob and globSync.

// Using glob and globSync for pattern matching
const { glob, globSync } = require('fs');
glob('**/*.js', (err, files) => {
  if (err) throw err;
  console.log(files);
});
const files = globSync('**/*.js');
console.log(files);

NodeJS 18 End of Life

The end of life for NodeJS 18 is coming in almost exactly one year from now. The official timeframe is in April 2025 – or better yet next year. People are highly recommending people to update their NodeJS applications to v20 or v22 before the end of life for NodeJS 18 comes.


Do you like what you're reading from the CoderOasis Technology Blog? We recommend reading our Implementing RSA in Python from Scratch series next.
Implementing RSA in Python from Scratch
Please note that it is essential for me to emphasize that the code and techniques presented here are intended solely for educational purposes and should never be employed in real-world applications without careful consideration and expert guidance. At the same time, understanding the principles of RSA cryptography and exploring various

The CoderOasis Community

Did you know we have a Community Forums and Discord Server? which we invite everyone to join us? Want to discuss this article with other members of our community? Want to join a laid back place to chill and discuss topics like programming, cybersecurity, web development, and Linux? Consider joining us today!
Join the CoderOasis.com Discord Server!
CoderOasis offers technology news articles about programming, security, web development, Linux, systems admin, and more. | 112 members
CoderOasis Forums
CoderOasis Community Forums where our members can have a place to discuss technology together and share resources with each other.