Node.js 19’s New Watch Mode and Other Features You Should Know About


In October 2022, Node.js released its 19th version. It came six months after the release of Node.js v.18, which featured significant upgrades such as the native test runner module.


A notable feature in Node.js 19 is the new and experimental watch mode. This allows you to make changes to your server without having to restart it.

Before Node.js 19, watch mode was only possible using third-party libraries. Learn how to use the watch mode and explore other updates to Node.js in version 19.


Node’s New Watch Mode

Node.js 19 introduces the experimental –watch flag, which restarts a Node.js server when it detects changes in the specified file. You could do this previously using a third-party library called nodemon, but the update removes the need for a dependency by adding its functionality to Node.js.

To restart your Node.js server using the –watch flag, run the node command with the –watch flag followed by the name of the file you want to restart when Node detects changes.

For example, consider this code block as a server.js file:

 
const express = require("express");
const app = express();
const PORT = 6060;

app.listen(PORT, () => console.log(`App listening on port: ${PORT}`));

To watch this file for changes, and restart the server when they occur, run this command on your terminal:

 node 

The command will watch your server.js file and restart the Node.js server when it detects changes made in the file.

It is worth noting that this feature is still experimental, which means you may experience issues while using the –watch flag to restart your server.

Other Upgrades and Fixes

Other improvements and fixes that came with Node.js 19 include the following.

HTTP Keep-Alive by Default

The http/https module’s keepAlive option controls whether it should keep a connection to a server after it has completed a request. Originally, you would have to set the keepAlive option to true manually. This option instructs the server to keep the connection open and reuse it for subsequent requests.

In Node.js 19, the keepAlive option is set to true by default. This addition will significantly reduce the overhead of establishing new connections.

Stabilization of the WebCrypto API

The WebCrypto API is Node.js’s implementation of the Web Crypto API standard. With Node.js 19, the WebCrypto implementation is now stable, except for the Ed25519, Ed448, X25519, and X448 algorithms.

You can access this API using the globalThis module or the node: prefix that Node.js 18 introduced to differentiate core Node.js modules from third-party libraries.

For example:

 
const crypto = require("node:crypto");


const webcrypt = globalThis.crypto;

Custom ESM Resolution Adjustments

Node.js 19 removes the previously experimental –experimental-specifier-resolution flag. This provided experimental support for locating files using package specifiers, similar to how ECMAScript imports modules.

Node.js removed this flag because you can replicate their functionality using custom loaders. You can use custom loaders to support more module formats or to process modules further before loading since they let you provide your unique logic for loading and processing modules.

Removal of DTrace/SystemTap/ETW Support

DTrace, SystemTap, and ETW (Event Tracing for Windows) are modules that offer dynamic tracing and analysis of running programs. Originally in Node.js, you could use them to gather data on an application’s activity, including performance indicators, errors, and other possible runtime occurrences.

In Node.js 19, Node.js removed support for DTrace, SystemTap, and ETW because the complexity involved in maintaining and keeping these modules up-to-date has yet to be worth it. So, to prioritize their resources, they dropped support for them.

Upgrading to Node.js 19

Node.js 19 introduces various features and improvements worth considering for your application, including the new watch mode, among others discussed above. To upgrade to Node.js 19, head to the official website and download the current version.



Source link

Share

Leave a Reply

Your email address will not be published. Required fields are marked *