Everything about the latest Node.js release | Node.js 16

Node.js v16 is here!

But wait, I just upgraded my project to Node 12 and you’re telling me that a new version is already released! I haven’t even upgraded my project to node 14 or 15! Should I directly go to note 16 now? But wait what are the breaking changes? Also, what are the new features? Is there any benefit of even upgrading? Should I even think of it?

If you ever had any of these questions don’t worry about it as I’m here to tell you everything you need to know about node.js latest version if you need to upgrade to it, what are the breaking changes and what are the new features.

Let me first address the elephant in the room: Do you need to upgrade your project to node 16? The answer would be NOT YET! Node.js 16 will be the ‘Current’ release for the next 6 months and then promoted to Long-term Support (LTS) in October 2021.

Node.js 12 will remain in long-term support until April 2022, and Node.js 14 will remain in long-term support until April 2023. Node.js 10 will go End-of-Life at the end of this month (April 2021).

Whenever Node.js comes out with an odd number release it is not going to be promoted to LTS and whenever it comes out with an even number release that is what you should really upgrade your applications to because it will go to long-term support and it will stay for a long time.

Summary of the major changes:

  1. Node.js v16.0.0 is the first release to have prebuilt binaries for Apple Silicon shipped with it.

  2. Experimental implementation of the standard Web Crypto API

  3. Abort controller: It’s was an experimental implementation introduced in Node.js v15, very useful and is now stabilized in Node v16. It allows us to reject or abort any kind of promise-based API.

  4. Npm 7: npm v7.10.0 in Node.js v16.

  5. V8 9.0: v8 javascript engine has been upgraded to 9.0. For those who don’t know 8.6 was already released in Node.js v15.
    - ECMAScript RegExp Match Indices
    - Performance Improvements
    - Stable Timers Promises API

  6. Breaking Changes - None

Web Crypto API

Before Node.js v16, external libraries were required to be installed in order to perform cryptographic operations which raised questions on their efficiency.

Cryptographic operations entail encryption and description of all sorts, hashing, signature generation, multi-factor authentication, etc.

Node.js v16 brings built-in support for such operations which makes it easy and efficient to carry out such operations using Node.js.

Read more about Web Crypto API here.

AbortController

This is very cool because before this we could only unsubscribe to the API calls we have already made but we didn’t really abort them they were still executed we just didn’t listen to the response. With AbortController we can actually abort any kind of API request: it can be a response body type or it can even be a stream (like downloading a video).

var controller = new AbortController();
var signal = controller.signal;

var downloadBtn = document.querySelector('.download');
var abortBtn = document.querySelector('.abort');

downloadBtn.addEventListener('click', fetchVideo);

abortBtn.addEventListener('click', function() {
  controller.abort();
  console.log('Download aborted');
});

function fetchVideo() {
  ...
  fetch(url, {signal}).then(function(response) {
    ...
  }).catch(function(e) {
    reports.textContent = 'Download error: ' + e.message;
  })
}

First we need to create a controller using the abort controller constructor and then grab a reference to its associated signal using the controller.signal property.

Now we have two buttons in the HTML which are basically a download button and an abort button. On the download button, we add an event that basically listens to a click event and whenever that happens it starts fetching the video. On the other hand, the abort button also listens to a click event but it actually aborts the controller, and then it logs “Download aborted”.

Read more about AbortController here.

AbortController Live Example

ECMAScript RegExp Match Indices

This provide the start and end indices of the captured string. The indices array is available via the .indices property on match objects when the regular expression has the /d flag.

> const matchObj = /(Java)(Script)/d.exec('JavaScript');
undefined

> matchObj.indices
[ [ 0, 10 ], [ 0, 4 ], [ 4, 10 ], groups: undefined ]

> matchObj.indices[0]; // Match
[ 0, 10 ]

> matchObj.indices[1]; // First capture group
[ 0, 4 ]

> matchObj.indices[2]; // Second capture group
[ 4, 10 ]

Stable Timers Promises API

The Timers Promises API provides an alternative set of timer functions that return Promise objects, removing the need to use util.promisify().

import { setTimeout } from 'timers/promises';

async function run() {
  await setTimeout(5000);
  console.log('Hello, World!');
}

run();

I hope this article helped you to understand the latest Node.js release. Thanks for reading. If you have any questions, feel free to leave a response.

Resources