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

Hope your code doesn't break!!

Nodejs v15 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! Should I directly go to note 15 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.


Summary of the major changes:

  1. Abort controller: It's basically an experimental implementation but it's very useful. It basically helps us to reject or abort any kind of promise-based API

  2. Npm 7: There's a new npm version major release that came out and it has some changes in the package-lock.json format and also it introduced npm workspaces.

  3. V8 8.6: v8 javascript engine has been upgraded to 8.6. For those who don't know 8.4 was already released in note 14.

    • Promise.any()

    • AggregateError

    • String.prototype.replaceAll()

    • &&=, ||= and ??=

  4. Breaking Changes


Let me first address the elephant in the room: Do you need to upgrade your project to node 15? The simple answer is NO! Don't be surprised as it's an odd-numbered release line and node js 15 will not be promoted to LTS (Long Term Support).

Whenever NodeJs 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.


AbortController

It's basically a new experimental implementation that allows us to cancel any kind of promise-based API.

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;
  })
}

Let's go through the basic code real quick in order to actually understand what's happening behind the scenes.

So 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”.

I'm sure you understand at least a little bit of it as I said it's still experimental so I don't recommend you start using this in your production applications.

Read more about AbortController here.

AbortController Live Example


NPM 7

We won't dive deep into this change because this actually needs an article of its own. I would just give you a brief overview of what changed and what are the new features!

For those who don't know what's NPM: it's used by millions of developers, it has millions of libraries and it has about 75 billion downloads a month so you really need to check it out.

New features:

  1. Workspace: A workspace that now helps us to manage multiple projects within a single top-level package.

  2. Automatically install peer dependencies: Now it automatically installs the peer dependencies. From NPM 7 developers will need to manage their own peer dependencies. This might be a breaking change for a few applications. Now if you're thinking about what peer dependency is, I highly recommend you to go through the docs but I'll give you a brief overview: let's say you have a library or you have an NPM package that is published and it has some peer dependencies. Now before this release, your package did not have to install any of the peer dependency. Any application or any service that uses your package needs to install it.


Promise.any() and AggregateError

Promise.any() basically takes a number of promises and it resolves as soon as one of the promises is completed.

It is the exact opposite of promise.all().

In promised.all() we used to pass a number of promises but it only used to resolve when each and every promise that we have passed is resolved. On the other hand, in this case we wait for anyone of them to resolve so it's the exact opposite of promised.all()

Now you must be thinking about what happens if all of the promises we have passed to promise.any() fail! Well in that case it will return an AggregateError which is a new subclass of error which is also released with v8 8.6.

const promise1 = Promise.reject(0);
const promise2 = new Promise((resolve) => setTimeout(resolve, 100, 'quick'));
const promise3 = new Promise((resolve) => setTimeout(resolve, 500, 'slow'));

const promises = [promise1, promise2, promise3];

Promise.any(promises).then((value) => console.log(value));

// expected output: "quick"

In the example over here, we have three promises. The first one is rejected as you can see. The second one resolves in 100 milliseconds and the third one resolves in 500 milliseconds.

Now we group them in an array and we pass and promise.any() and it will resolve as soon as the first promise is resolved and will return us the value of the promise that has resolved the first so when we run it you can see that it prints “quick”.

So basically it didn't wait for the one with 500 milliseconds delay and it didn't reject because one of the promises was rejected. It basically returned us the value of the first promise that was completed successfully.


String.prototype.replaceAll()

The replaceAll() method returns a new string with all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp. The original string is left unchanged.

const p = 'The quick brown fox jumps over the lazy dog.';

console.log(p.replaceAll('dog', 'monkey'));

// expected output: "The quick brown fox jumps over the lazy monkey."

As you can see in this example we have a string over here and we want to replace all the “dog” with “monkey”.


Finally, NodeJs has been with us for a little over 10 years now and they have done nothing but improved themselves and they plan to do the same in the next 10 years and they have already laid out a roadmap for the next 10 years. They have taken it to a whole another level this time they started taking surveys of the contributors and they started taking into consideration the changes for the features that are the most requested.


I hope this article helped you to understand the latest NodeJS release.

Thanks for reading. If you have any questions, feel free to leave a response.


Resources

  1. https://nodejs.org/en/blog/release/v15.0.0

  2. https://developer.mozilla.org/en-US/docs/Web/API/AbortController

  3. https://github.blog/2020-10-13-presenting-v7-0-0-of-the-npm-cli/

  4. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any

  5. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AggregateError

  6. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll