Node.js 19 Release: What’s New
Tags: node
Published: 2022-11-22 14:46:24

The Node.js team recently announced the release of version 19. This has the following features:

- `node --watch` (experimental)
- KeepAlive by default
- Stable WebCrypto
- V8 engine updates

In this article, we will explore the major highlights of this release. We recommend downloading the latest [Node.js 19 release](https://nodejs.org/en/download/current) to follow along.

To verify your version of node:

```shell
> node --version
```

Let's get started!

## `node --watch` (Experimental)

This exciting new feature adds a file watcher using the `--watch` option. Running a Node.js app in watch mode automatically restarts the process when an imported file changes.

To check out this new feature, create two files: `index.mjs` and `import.mjs`. This feature only works with Node.js v19.0.0 and v18.11.0+.

In the `import.mjs` file write:

```javascript
export const message = "Hello, World";
```

Then, in the `index.mjs` file:

```javascript
import { message } from "./import.mjs";

console.log(message);
```

Now run:

```shell
> node --watch index.mjs
```

The tool should output that it is running in watch mode. Keep in mind this is an experimental feature so it could change at any time.

Update the `import.mjs` file on the fly by changing the `message`:

```javascript
export const message = "Hello, New World";
```

Note the process restarts, and the new message now appears on the console. The possibilities here are endless, as you can focus more on your code without worrying about the running process.

For the remainder of this post, run the `node` executable by itself. This loads a REPL we can use to check out the rest of the new features.

```shell
node
```

## HTTP(S)/1.1 `keepAlive` by Default

With this release, Node.js sets the `keepAlive` header by default, and the duration is set to 5 seconds. This reuses HTTP(S) 1.1 connections, increasing throughput because the header instructs the client on how long to stay connected. The server will also automatically disconnect any idle connections when `close()` is invoked.

To check out this new feature, create an HTTP server:

```javascript
const http = require("http");

const requestListener = function (req, res) {
  res.writeHead(200);
  res.end("Hello, World!");
};

const server = http.createServer(requestListener);
server.listen(8080);
```

You can copy-paste this code into the REPL or type it all out. Once the server is running, run a CURL command:

```shell
> curl -i http://localhost:8080
```

Note the header on the response - `Keep-Alive: timeout=5`. This instructs the client to reuse connections and keep them open for 5 seconds.

## Stable Web Crypto API

The [Web Crypto API](https://nodejs.org/api/webcrypto.html#web-crypto-api) is now stable. You can access this module via `globalThis.crypto` or `require('node:crypto')`.

To check out Web Crypto API, try to encrypt - then decrypt - a secret message:

```javascript
const { subtle, randomBytes } = require("node:crypto");

const iv = randomBytes(16);
const key = await subtle.generatedKey(
  {
    name: "AES-CBC",
    hash: "SHA-256",
    length: 256,
  },
  true,
  ["encrypt", "decrypt"]
);

const secretMessage = "I love node!";
const encrypted = await subtle.encrypt(
  {
    name: "AES-CBC",
    iv: iv,
  },
  key,
  secretMessage
);
const dencrypted = await subtle.decrypt(
  {
    name: "AES-CBC",
    iv: iv,
  },
  key,
  encrypted
);

const dec = new TextDecoder("utf-8");
console.log(dec.decode(decrypted));
```

You can also use this same API to generate symmetric and asymmetric keys (public and private keys) for your specific security needs. Please note the Ed25519/Ed448/X25519/X448 algorithms are still experimental.

## V8 Engine 10.7 Updates with `Intl.NumberFormat` JavaScript API

The V8 engine has been upgraded to version 10.7, which is part of Chromium 107. This version includes a new JavaScript API: `Intl.NumberFormat`.

This [JavaScript API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat) enables language-sensitive number formatting.

```javascript
const number = 123456.789;

const china = new Intl.NumberFormat("zh-CN", {
  style: "currency",
  currency: "CNY",
});
console.log(china.format(number)); //¥123,456.79

const iraq = new Int.NumberFormat("ar-IQ", {
  style: "currency",
  currency: "IQD",
});
console.log(iraq.format(number)); //١٢٣٬٤٥٧د.ع

const us = new Int.NumberFormat("en-US", {
  style: "currency",
  currency: "USD",
});
console.log(us.format(number)); //$123,456.79
```

The `Intl.NumberFormat` object has a nice long list of [options to support international number formatting](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#options).

## Node.js 18 Promoted to LTS

Node.js 19 will not be promoted to LTS because only even-numbered releases are promoted to LTS. For production workloads, we recommend LTS releases. However, it is still a good time to play with Node.js 19 and provide feedback to help pave the way for future releases.

As Node.js 18 is being promoted to Active LTS, it is the recommended release for production deployments. According to the [release schedule](https://github.com/nodejs/release#release-schedule), Node.js 18 reaches end-of-life in April 2025, meaning three years of long-term support and bug fixes before then.

Node.js 18 has features that make upgrading a good choice:

- Experimental fetch
- Experimental test runner
- ECMASCript improvements
- V8 upgrade to 10.1

Check out more details in our previous blog, [Node.js 18 Release: What’s New](https://blog.appsignal.com/2022/04/26/nodejs-18-release-whats-new.html).

## Wrap Up: Ecosystem Implications and Node.js 19 Release Notes

Node.js 19 continues to narrow the gap between the browser and the server. This is great news for developers who wish to work on the full stack without context-switching between languages.

The experimental watch tool is also exciting because it aids developer productivity within the platform. We hope to see more of these types of improvements in the future.

Stabilizing APIs, upgrading V8, and improving performance via KeepAlive are all signs of a healthy ecosystem that promises to deliver more features in future releases.

To see the full list of changes, be sure to look at the [official Node.js 19 release notes](https://nodejs.org/en/blog/announcements/v19-release-announce/).

**P.S. If you liked this post, [subscribe to our JavaScript Sorcery list](https://blog.appsignal.com/javascript-sorcery) for a monthly deep dive into more magical JavaScript tips and tricks.**