Node.js 21.0.0 Release: What’s New and Exciting
Node.js, the popular JavaScript runtime, has just released its latest version, 21.0.0, on October 17, 2023. This version brings many new features and improvements that will enhance your development experience and productivity. Here are some of the highlights of this release:
1. V8 JavaScript Engine Update
Node.js 21.0.0 includes an update to the V8 JavaScript engine, now at version 11.8. This update brings the latest performance optimizations and language features, including private class fields and logical assignment operators. Here are some examples of what you can do:
// Private class fields
class MyClass {
#privateField = 42;
getPrivateField() {
return this.#privateField;
}
}
const myInstance = new MyClass();
console.log(myInstance.getPrivateField()); // Output: 42// Logical assignment operators
let value = 5;
value &&= 10; // Equivalent to: value = value && 10;
console.log(value); // Output: 102. Stable Fetch and WebStreams
If you’ve been using the experimental fetch and WebStreams APIs in Node.js, you’ll be pleased to know that they are now stable and ready for production use. Fetch and WebStreams provide a modern and consistent way to interact with HTTP resources and streams in Node.js, following web standards. Here’s an example of using the API:fetch
const fetch = require('node-fetch'); // Note that you still use 'require' for 'node-fetch'
async function fetchData() {
try {
const response = await fetch('https://jsonplaceholder.typicode.com/posts/1');
const data = await response.json();
console.log(data);
} catch (error) {
console.error(error);
}
}
fetchData();3. Experimental Flag for CommonJS to ES Modules
Migrating from CommonJS to ES modules in Node.js can be challenging due to ambiguous code interpretation. To assist with this transition, Node.js 21.0.0 introduces a new experimental flag, , that changes the default interpretation of ambiguous code from CommonJS to ES modules. This means you can use and syntax without altering your file extensions or adding to your . For instance:--cjs-to-esmimportexport"type": "module"package.json
Before (CommonJS):
const foo = require('./foo');
console.log(foo);After (ES Modules with flag):--cjs-to-esm
import foo from './foo';
console.log(foo);Updates to Test Runner: Node.js 21.0.0 also brings several updates to its test runner, which is used for testing the Node.js core itself. These updates include improved support for parallel testing, enhanced reporting, and more.
These are just some of the exciting changes that Node.js 21.0.0 has to offer. You can read the full changelog here. If you want to try out this version, you can download it from the official website here. Happy coding!

