What is new in Node.js V22.5

A brief introduction about the new tool

nairihar
3 min readJul 17, 2024
Generated with Leonardo AI

In this article, we will review the notable changes introduced in the new Node.js version:

  • add node:sqlite module (Colin Ihrig) #53752
  • add matchesGlob method (Aviv Keller) #52881
  • add postMessageToThread (Paolo Insogna) #53682

Let’s get started!

node:sqlite

The node:sqlite module facilitates working with SQLite databases.

// index.js

const { DatabaseSync } = require('node:sqlite');
const database = new DatabaseSync(':memory:');

// Execute SQL statements from strings.
database.exec(`
CREATE TABLE data(
id INTEGER PRIMARY KEY,
name TEXT
);
`);

// Create a prepared statement to insert data into the database.
const insert = database.prepare('INSERT INTO data (id, name) VALUES (?, ?)');

// Execute the prepared statement with bound values.
insert.run(1, 'Bob');
insert.run(2, 'John');

// Create a prepared statement to read data from the database.
const query = database.prepare('SELECT * FROM data ORDER BY id');

// Execute the prepared statement and log the result set.
console.log(query.all());

// [ { id: 1, name: 'Bob' }, { id: 2, name: 'John' } ]

The following example shows the basic usage of the node:sqlite module to open an in-memory database, write data to the database, and then read the data back.

This built-in lib becomes available when using the --experimental-sqlite flag.

node --experimental-sqlite index.js

We developers, along with Node.js native modules, use many external modules to make the server fully functional. It’s excellent that Node.js tries to include those important tools on its own. For example, they recently added a native support of test runner, so we switched from jest and mocha to the native library. Now, it's time for the databases and ORMs.

This is currently an experimental module, and it will take some time for it to become stable and have more methods.

Let’s appreciate the Node.js Core team’s effort. One day, we will only use a small number of external libraries as most key modules will be available natively.

matchesGlob

This is another experimental method which determines if path matches the pattern.

path.matchesGlob('/foo/bar', '/foo/*'); // true
path.matchesGlob('/foo/bar*', 'foo/bird'); // false

postMessageToThread

Last but not least, a method that sends a value to another worker, identified by its thread ID.

Previously, we could communicate with the worker threads using Message Channels. But now, thread communication is much simpler.

Here is how we can get the thread ID:

const { threadId } = require('node:worker_threads');

And this is how they can communicate:

postMessageToThread(id, { message: 'pong' });

To be able to get the messages first, you need to have the listener:

process.on('workerMessage', (value, source) => {
console.log(`${source} -> ${threadId}:`, value);
});

The source is the sender's thread ID, so you can do something like this if you want to message back.

process.on('workerMessage', (value, source) => {
console.log(`${source} -> ${threadId}:`, value);
postMessageToThread(source, { message: 'pong' });
});

If you want to communicate with the main thread, you can simply use 0, as the main thread’s ID is 0.

postMessageToThread(0, { message: 'ping' });

Full example here.

I hope you enjoyed this summary. I aimed to deliver the information promptly and highlight the most important updates.

Also, follow my JavaScript newsletter on Telegram: @javascript

--

--

nairihar

Sharing valuable insights on the latest tools and trends in JavaScript. nairi.dev