6 New Features Of Node.js 18 Every Developer Should Know

Node.js 18 recently became the LTS version of Node.js. Node.js 18 came with 6 new features every developer should know.

Posted by Saportif Technology on

What is Node.Js?

Node.js is an open-source, usually server-sided cross-platform JavaScript runtime environment. Every six months a major update is released. Node.js 18 was released on 19 April 2022 and it came with some handy features. Node.js 18 is the current LTS version.

What is LTS?

LTS is the abbreviation of Long Time Support which means the LTS version will be maintained and supported for a longer period of time. Priority of LTS version stability and reliability. Node.js implements LTS by making its even numbered releases LTS version and recommends this version to users. As of Node.js 18, this release became the LTS version.

As you can see in the picture, Node.js 18 became active in October which means it now is the LTS version. Node.js 19 became the current version at the same time. But, that release is intended for library owners such as React, Gulp, Webpack in order to let them make their tools compatible earlier. After 6 months, that version will become unsupported.


How to Use Node.js 18?

Node.js versions can easily be installed and changed by using nvm which is a version manager for Node.js. 

You can install nvm or nvm-windows.

Example:

$ node -v
 v16.17.1
 $ nvm install 18
 Downloading node.js version 18.12.1 (64-bit)...
 $ nvm use 18.12.1
 Now using node v18.12.1 (64-bit) 

New Features

Experimental Built-in Fetch API

‘node-fetch’ is one of the most popular modules that is used to perform HTTP requests. In order to use ‘node-fetch’, you needed to use npm and install the module ‘node-fetch’. With v18, this is no longer necessary. Node.js 18 provides an experimental native fetch API to users programmers.

fetch syntax:

function fetch(input: RequestInfo, init?: RequestInit): Promise<Response>

input represents the URL string and init represents the optional parameter.

fetch example:

Web Streams API

Prior to this update, you would need to wait for the entire source to be downloaded in order to process it. Web Streams API significantly improves this procedure by letting us access the data while it is being read. This process is performed by accessing streams of the data and processing them by chunks. With v18, the following Web Stream APIs is available on the global scope.

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


Experimental node:test

With ‘node:test’ module, you can write unit tests and report them in TAP (Test Anything Protocol) format. ‘node:test’ is an alternative to libraries such as Jest or Mocha, but it’s advantage is that, it is not an addititional third-party library like Jest or Mocha.

node:test example: 

In this example, the first test passes because 5 = 5. The second test fails because 5 ≠ 10.

Prefix-Only Core Modules

Node.js v18 brought us a new way to import modules by adding a ‘node:’ prefix. This features importance is that, it makes it obvious to the user that the imported module is from Node.js core. The ‘node:test’ module is an example to prefix-only core modules.

Experimental --watch Property

Almost all of web developers that use node.js also know about or use nodemon. Nodemon is a tool that simply restarts the node application when a change happenes to the file directory. ‘--watch’ property replaces nodemon which means that there is no need to install an additional third-party tool.

--watch example:

New V8 JavaScript Features

Node.js uses V8 Javascript engine. There are new features in V8 as well. These new features include:

  • Array methods: findLast and findLastIndex
  • Internationalization support: Intl.Locale and Intl.supportedValuesOf
  • Class field and private class method improvements.