what is new in node js version 18 [new features]

NodeJS 18 latest futures 🤯 [new features to explore]

Stanalone Test Runner, Fetch (Experimental) Support, V8 Engine (not a car) and more!

NodeJS has released the new version of NodeJS, which is named NodeJS 18 and it offers plenty of new features for developers to take advantage of. These new features are quite useful to every developer as they help in improving productivity and increasing performance as well. This article will talk about some of the important new features that you should start using if you’re running an existing application on NodeJS or want to start building one from scratch.

Node.js 18 has updated the V8 JavaScript engine to version 10.1, which comes from Chromium 101. Thanks to that update, the findLast() and findLastIndex() vector methods, the Intl.supportedValuesOf function, improvements to the Intl.Locale API and better performance of class fields and private class methods have been introduced.

NodeJS Roadmap (https://nodejs.org/en/about/releases/)

Standalone Test Runner

Testing is of vital importance in our systems and it is the right way in which we can manage the risk of errors occurring in production, normally to write tests in NodeJS we use very common libraries like Jest, AVA, Supertest, MochaJS, etc... But now from version 18, NodeJS has a native way to implement tests, the way to use this new API is as follows:

import test from 'node:test';
import assert from 'node:assert';

test('synchronous passing test', (t) => {
  // This test passes because it does not throw an exception.
  assert.strictEqual(1, 1);
});

test('synchronous failing test', (t) => {
  // This test fails because it throws an exception.
  assert.strictEqual(1, 2);
});
import test from 'node:test';
import assert from 'node:assert';

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

It is also possible to use the context of the test() function to create sub tests, like this one:

You must keep in mind that this particular test module has the node: prefix when importing it, if you don’t do it this way NodeJS will try to look for the test package as an external package inside your node_modules.

Fetch Experimental Support

This is the feature that the community has liked the most since we can finally use the fetch function within the context of NodeJS. Remember that JavaScript can run directly in a browser and in that context it is very natural to use the fetch function to make requests, for example to some API as follows:

With the code above you can make a request and be able to get the result, you can also implement it using the [async/await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) mode, like this example:

async function getProducts() {
  const response = await fetch('https://api.site.com/api/v1/products');
  const data = await response.json();
  console.log(data);
}

const res = await fetch('https://api.site.com/api/v1/products');
if (res.ok) {
  const data = await res.json();
  console.log(data);
}

In the examples seen above we see the most normal way to do it but it only works in browsers, i.e. it could not be used in other contexts, for example on the server side where we also want to make requests or connect to other services via fetch.

But now in version 18 they are working on experimental support so that [fetch](https://nodejs.org/en/blog/announcements/v18-release-announce/#fetch-experimental) can be executed. Let’s check an example with NodeJS:

So now we will be able to make requests using fetch as in the browsers.

V8 Engine Version 10.1

I don’t know why but when I read V8 engine, this immediately came to my mind:

A non NodeJS V8 Engine

Sorry… Let’s continue

In this version 18 of NodeJS, version 10.1 of the V8 engine is supported, which brings the following features:

  • Added [findLast](https://v8.dev/features/finding-in-arrays) and [findLastIndex](https://v8.dev/features/finding-in-arrays) methods on arrays.
  • More Improvements in [Intl.Locale](https://v8.dev/blog/v8-release-99#intl.locale-extensions) API.
  • The [Intl.supportedValuesOf](https://v8.dev/blog/v8-release-99#intl.locale-extensions) function.

https://nodejs.org/en/blog/announcements/v18-release-announce/

https://developer.mozilla.org/JavaScript/Statements/async_function

https://nodejs.org/api/v8.html

https://v8.dev/features/

Comments