We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

What’s new in Node.js 20?

Node.js has been an important tool for web developers for over a decade, and it continues to progress and improve with each new version. In this post, we’ll look at some of the main improvements in Node.js 20 and how they may help developers.

Every six months, Node.js rolls out a major release that becomes the Current release, giving library authors time to add support. After six months, odd-numbered releases become unsupported, and even-numbered releases move to Active LTS, ensuring general usability. Active LTS or Maintenance LTS are recommended for production applications, as they guarantee critical bug fixes for up to 30 months.

Node.js 20 was released on April 18, 2023. It becomes the current release. It includes six important features:

  • Permission Model
  • Custom ESM loader hooks nearing stable.
  • Synchronous import.meta.resolve()
  • url.parse() warns URLs with ports that are not numbers.
  • Stable Test Runner
  • V8 JavaScript engine updated to V8 11.

Install or Upgrade NVM

To install or update nvm, you should run the install script from the official NVM repository in Github. To do that, you may either download and run the script manually, or use the following cURL or Wget command. Here we will use the cURL command.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash

The above command will install NVM.

Now you need to add the source lines to your profile.

source ~/.bashrc

Now you can use the nvm command and explore latest Node.js 20.0.0

Use NVM To Explore Node

Current Version of Node.js 20.0.0 (includes npm 9.6.4)

Download the Node.js source code or a pre-built installer for your platform, and start developing today.

Node.js

Run the following command to install node 20.0.0:

% nvm install 20.0.0
Downloading and installing node v20.0.0...
Downloading https://nodejs.org/dist/v20.0.0/node-v20.0.0-darwin-x64.tar.xz...
################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v20.0.0 (npm v9.6.4)

Run the following command in any window to utilize node 20:

% nvm use 20.0.0
Now using node v20.0.0 (npm v9.6.4)

We’re now ready to explore:

% node --version
v20.0.0

Permission Model

The Permission Model is a new experimental feature in Node.js. It enables programmers to limit access to specified resources like as file system operations, child process spawning, and worker thread generation during program execution. The API is hidden behind the --experimental-permission switch, which when activated restricts access to all accessible permissions.

The Node.js Permission Model is an experimental technique for controlling execution-time access to certain resources.

The following features are included in this first edition of the Permission Model:

When Node.js is started with --experimental-permission, the ability to access the file system, launch processes, and use node:worker_threads is limited.

With the addition of the --allow-fs-read and --allow-fs-write parameters, Node.js developers now have additional control over file system access. These experimental capabilities provide more granular control over which areas of the file system Node.js processes may access.

Custom ESM loader Hooks Nearing Stable

Custom ES module lifecycle hooks provided by loaders (--experimental-loader=./foo.mjs) now operate on a separate thread that is separate from the main thread. This gives loaders their own scope and prevents cross-contamination between loaders and application code.

Import.meta.resolve() now returns synchronously, in line with browser behavior; however, resolve hooks in user loaders can remain async if the loader author prefers, and import.meta.resolve will still return synchronously in application code.

These were the final tasks to be completed before declaring ESM loaders stable. We hope to mark the loaders flag, import.meta.resolve, and the resolve and load hooks as stable when some time has passed with no serious issues discovered by the community. As a result, major constituencies such as instrumentation suppliers will have a reliable API on which to create analytics and reporting libraries, allowing for more broad use of ESM.

Synchronous import.meta.resolve()

This method now returns synchronously, in accordance with browser behaviour. Despite this, user loader resolve hooks can still be specified as async functions (or, if the author wishes, sync functions). Even if async resolve hooks are loaded, import.meta.resolve returns synchronously for application code.

URLs with ports that are not numbersurl.parse() warns

URLs with non-number ports are accepted by url.parse(). This might lead to hostname spoofing with unexpected input. These URLs, like the WHATWG URL API, will produce an error in future versions of Node.js. These URLs cause url.parse() to generate a warning starting with Node.js 20.

Stable Test Runner

Version 20 of Node.js introduces a significant modification to the test_runner module. Following a recent upgrade, the module has been designated as stable. The stable test runner comprises the following components for creating and executing tests:

  • describe, it/test and hooks to structure test files
  • mocking
  • watch mode
  • node --test for running multiple test files in parallel

V8 JavaScript engine updated to V8 11

The V8 engine has been upgraded to Chromium 113 version 11.3. The JavaScript API has three new features in this version:

  • String.prototype.isWellFormed and toWellFormed
  • Methods that change Array and TypedArray by copy
  • Resizable ArrayBuffer and growable SharedArrayBuffer
  • RegExp v flag with set notation + properties of strings
  • WebAssembly Tail Call

Key Features

  • enhanced performance: Node.js 20 improves on prior versions in terms of speed, with greater support for current JavaScript features and quicker startup times.
  • Increased security: Node.js 20 has better security features such as improved TLS 1.3 compatibility and stronger cryptography for safe connections.
  • Better TypeScript support: Node.js 20 improves TypeScript support, making it easier to utilize the popular TypeScript language while creating Node.js applications.
  • Better debugging experience: Node.js 20 has a new debugging experience that makes debugging and troubleshooting Node.js applications easier.
  • Better error handling: Node.js 20 has enhanced error handling, allowing developers to catch and manage faults in their applications more easily.

I hope this article helps you understand what’s new in Node.js 20. We trust that it has been helpful to you. Please feel free to share your thoughts and feedback in the comment section below.

Continue Reading

Cart

Your Cart is Empty

Back To Shop