What is new in the Node.js 18?
Recently the Node.js team has released a newer version after their team spent a lot of time and effort on strengthing the core, and guess what it is an even number!
What’s with version ends in even number?
All the even numbered versions will be there on the LTS list. LTS stands for Long Term Support. This Node.js v18 will be in 'Current' for the next 6 months and then will be moved to LTS in Oct 2022.
What is Hydrogen?
The LTS version of Node.js will be designated with the codename ‘Hydrogen’ and this version will have support until April 2025.
What are the newest features available in Node.js v18?
- Fetch
- Web Streams API
- Test runner module
- V8 engine upgrade to 10.1
What is Fetch in Node.js v18?
A Node.js application developer will know the pain of installing different packages for fetching the responses from other APIs and processing the result. But Node.js v18 has addressed the concern by making this available in the default package and so no need to install separately. At this point of time Fetch is experimental until more test coverage is verified.
Example
const res = await fetch('https://nodejs.org/api/documentation.json');
if (res.ok) {
const data = await res.json();
console.log(data);
}
What is Web Streams API in Node.js v18?
Some of the experimental Web Stream APIs are now available on the global scope. This would mainly help to 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
- Blob
- BroadcastChannel
What is a Test runner module?
Again, for testing the Node.js application developed, several packages are available but from this release, the application can be tested with inbuilt module. To consume this module, follow the below
import test from 'node:test';
test('top level test', async (t) => {
await t.test('subtest 1', (t) => {
assert.strictEqual(1, 1);
});
await t.test('subtest 2', (t) => {
assert.strictEqual(2, 2);
});
});
What is with V8 engine upgraded to version 10.1 in Node.js v18?
Node.js runs with the V8 engine from the Chromium open source browser. This engine is upgraded to version 10.1 which is part of the recent update in Chromium 101.