MWAN MOBILE

×
mwan_logo
blog-banner

11 Features in Node.js 18 you need to try

Programming 18-Nov-2022

Node.js 18 LTS is now available. What’s new?

Node.js 18 was released on the 19th of April this year. You can read more in the official blog post release or in the OpenJS Blog announcement. The community couldn’t be more excited!

Here at NodeSource,releases are a big deal. As a team of experts, enthusiasts, and core contributors to the open-source project, we love seeing the progress of Node! We are also one of the primary distributors of the runtime and have been since version 0.x (2014).

Developers download and use our binaries worldwide for their production environments (over 100m a year and growing!). We are incredibly proud to support this important piece of the Node ecosystem in addition to building and supporting customers on our Node.js platform – N|Solid.

“If you use Linux, we recommend using a NodeSource installer.” – From the NPM Documentation

If you want to lend a hand, we welcome your ideas or solutions contact us, or if you would like to help us continue supporting open source, you can contribute with an issue here.

Overall, the community is looking forward to this release with many new features and other benefits in addition to the official release earlier this year that included:

  • Security: Upgrading to OpenSSL 3.0
  • APIs: Fetch API is Promise based, providing a cleaner and more concise syntax.

If you are interested in thinking about the future of Node, we recommend checking out The next-10 group. They are doing some great work thinking about the strategic direction for the next 10 years of Node.js. Their technical priorities are:

  • Modern HTTP, WebAssembly, and Types.
  • ECMAScript modules and Observability
ContributorsOpenJS22
OpenJS Collaborator Summit 2022

But now I’m sure you want to get into the changes in v18. What has improved, and what are the new features? That’s what you’re here for 😉. So let us explain 👇

Hydrogen. What is it?

The codename for this release is ‘Hydrogen’. Support for Node.js 18 will last until April 2025. The name comes from the periodic table, and they have been used in alphabetical order (Argon, Boron, Carbon, Dubnium, Erbium…) 🤓 Read more in StackOverflow.

LTS?

According to the Node.js blog, the “LTS version guarantees that the critical bugs will be fixed for a total of 30 months and Production applications should only use Active LTS, or Maintenance LTS releases”. – https://nodejs.dev/en/about/releases/

In short, it focuses on stability and being a more reliable application after allowing a reasonable time to receive feedback from the community and testing its implementation at any scale.

Nodejs-releases-22
Nodejs Releases Screenshot 2022

How do I know what version of Node and LTS I have?

You can easily do it by typing in your console:

$ node --version

Run the following to retrieve the name of the LTS release you are using:

$ node -p process.release.lts

Note: The previous property only exists if the release is an LTS. Otherwise, it returns nothing.

If you want to be aware of the release planning in the Node.js community, you can check here: Node.js Release Schedule.

What’s new in Node.js 18?

Contributors are constantly working to improve the runtime, introduce more features, and improve developer experience and usability. Today as the worldwide community uses JS for developing API-driven web applications and serverless cloud development, the changes in this new LTS version are important to understand.

In honor of the number 11 (#funfact Undici means ‘eleven’ in Italian), we decided to make our top 11 Node.js 18 features:

  • Fetch API
  • 🧪- – watch
  • 🧪 OpenSSL 3 Support
  • 🧪 node:test module
  • Prefix-only core Modules
  • 🧪 Web Streams API
  • Other Global APIs: Blob and BrodcastChannel.
  • V8 Version 10.1
  • Toolchain and Compiler Upgrades
  • HTTP Timeouts
  • Undici Library

The idea of this blog post is to relevel the functionalities one by one, so let’s start:

Feature 1: Native Fetch API in Node.js 18

Finally, v18 provides native fetch functionality in Node.js. This is a standardized web API for conducting HTTP or other types of network requests. Previously Node.js did not support it by default. Because JavaScript is utilized in so many areas, this is fantastic news for the entire ecosystem.

Example:

nodejs18-native-fetch-API

Feature 2:--watch

Using --watch, your application will automatically restart when an imported file is changed. Just like nodemon. And you can use --watch-path to specify which path should be observed.

Also, these flags cannot be combined with --check--eval--interactive, or when used in REPL (read–eval–print loop) mode. It just won’t work.

You can now use Node Watch index on your file name to start watching your files without having to install anything.

Feature 3: OpenSSL 3 Support

OpenSSL is an open-source implementation of, among other things, SSL and TLS protocols for securing communication.

One key feature of OpenSSL 3.0 is the new FIPS (Federal Information Processing Standards) module. FIPS is a set of US government requirements for governing cryptographic usage in the public sector.

More information is available in the OpenSSL3 blog post.

Feature 4: The Experimental node:test

The node:test module facilitates the creation of JavaScript tests that report results in TAP (Test Anything Protocol) format. The TAP output is extensively used and makes the output easier to consume.

import test from node:test

This module is only available under the node:scheme. Read more in Node.js Docs

This test runner is still in development and is not meant to replace other complete alternatives such as Jest or Mocha, but it provides a quick way to execute a test suite without additional third-party libraries. The test runner supports features like subtests, test skipping, callback tests, etc.

node:test and --test

node:assert

The following is an example of how to use the new test runner.

nodejs18-nodetest

More information may be found in the Node.js API docs.

Feature 5: Prefix-only core Modules

A new way to ‘import’ modules that leverages a ‘node:’ prefix, which makes it immediately evident that the modules are from Node.js core

To learn more about this functionality, we invite you to read Colin Ihrig‘s article Node.js 18 Introduces Prefix-Only Core Modules.

Feature 6: Experimental Web Streams API

A Web Streams API is a set of streams API. Also experimental, it allows JavaScript to access streams of data received over the network programmatically and process them as desired. This means Stream APIs are now available on the global scope. This would help send the data packets in readable and writable streams.

Methods available are as follows,

  • ReadableStream
  • ReadableStreamDefaultReader
  • ReadableStreamBYOBReader
  • ReadableStreamBYOBRequest
  • ReadableByteStreamController
  • ReadableStreamDefaultController
  • TransformStream
  • TransformStreamDefaultController
  • WritableStream
  • WritableStreamDefaultWriter
  • WritableStreamDefaultController
  • ByteLengthQueuingStrategy
  • CountQueuingStrategy
  • TextEncoderStream
  • TextDecoderStream
  • CompressionStream
  • DecompressionStream

Feature 7: Other Global APIs

The following APIs in the Node v18 upgrade are exposed on the global scope: Blob and BroadcastChannel.

Feature 8: V8 Version 10.1

Node.js runs with the V8 engine from the Chromium open-source browser. This engine has been upgraded to version 10.1, which is part of the recent update in Chromium 101.

  • New array methods for finding the last element and index of an array. Array methods findLast and findLastIndex are now available.
  • Internationalization support: Intl.Locale and the Intl.supportedValuesOf functions.
  • Improving the performance of class fields and private class methods.
  • The data format of the v8.serialize function has changed (No compatible with earlier versions of Node.js.)

Keep an eye out here.

Feature 9: Toolchain and Compiler Upgrades

Node.js always provides pre-built binaries for various platforms. For every latest release, toolchains are evaluated and elevated whenever required. Node.js provides pre-built binaries for several different platforms. For each major release, the minimum toolchains are assessed and raised where appropriate.

  • Pre-built binaries for Linux are now built on Red Hat Enterprise Linux (RHEL) 8 and are compatible with Linux distributions based on Glibc 2.28 or later, for example, Debian 10, RHEL 8, Ubuntu 20.04.
  • Pre-built binaries for macOS now require macOS 10.15 or later.
  • For AIX, the minimum supported architecture has been raised from Power 7 to Power 8.

Note: Build-time user-land snapshot(Experimental)

Users can build a Node.js binary with a custom V8 startup using the –-node-snapshot-main flag of the configure script.

Feature 10: HTTP Timeouts

The http.server timeouts have changed:

  • headersTimeout (the time allowed for an HTTP request header to be parsed) is set to 60000 milliseconds (60 seconds).
  • requestTimeout (the timeout used for an HTTP request) is set to 300000 milliseconds (5 minutes) by default.

Feature 11: Undici Library in Node.js

Undici is an official Node team library, although it’s more like an HTTP 1.1 full-fledged client designed from the ground up in Node.js.

  • Keep alive by default.
  • LIFO scheduling
  • No pipelining
  • Unlimited connections
  • Can follow redirected (opt-in)

Of note, we support and love Lizz‘s work, so we recommend you check out her fantastic talk in Nodeconf.EU about New and Exciting features in Node.js to understand more about this feature.

Other Features/Changes:

The project undoubtedly has some great challenges in the near future to continue growing and maintaining its leading position in the ecosystem. These are some of the upcoming features. Most of them are experimental; without being the only ones to discuss, there is much work and proposals from an active community such as the Node.js Community.

  • Default DNS resolution
  • ECMAScript modules improvements
  • Improved support for AbortController and AbortSignal
  • Updated Platform Support
  • Async Hooks
  • Direct Network Imports
  • Build-time user-land snapshot
  • Support for JSON Import Assertions
  • Unflagging of JSON modules (experimental)
  • Support for HTTPS and HTTP imports
  • Diagnostic Channel
  • Trace Events
  • WASI

You can check the full changelog here.

Final Remarks

  • Node.js 12 will go End-of-Life in April 2022.
  • Node.js 14 (LTS) or Node.js 16 (LTS) or Later Node.js 18 will be LTS.
  • Node.js 18 will be promoted to Long-term Support (LTS) in October 2022 (NOW).
  • After being promoted to LTS, Node.js 18 will be supported until April 2025.

Upgrade Now!

Moving to the LTS version is the best decision for you to include the following improvements in your development workflow:

  • FetchAPI and Web Streams
  • V8: New advanced features, array methods, improvements, and enhancements.
  • Test runner without the need for third-party packages.
  • Deprecated APIs: Check the list here
  • Enhancement in Intl.Locale API.
  • Performance improvement in both class fields and private class methods.

Migration

To migrate your version of Node, follow these steps:

For Linux and Mac users, follow these commands to upgrade to the latest version of Node. The module n makes version management easy:

npm install n -g

For the latest stable version:

n stable

For the latest version:

n latest

Windows Users Visit the Node.js download page to install the new version of Node.js.

Source: Nodesource