avatarAustin - Kien Hoang

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1837

Abstract

js-variable language_">console</span>.<span class="hljs-title function_">log</span>(value); <span class="hljs-comment">// Output: 10</span></pre></div><h2 id="c7be">2. Stable Fetch and WebStreams</h2><p id="e14c">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:<code>fetch</code></p><div id="2a26"><pre><span class="hljs-keyword">const</span> fetch = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node-fetch'</span>); <span class="hljs-comment">// Note that you still use 'require' for 'node-fetch'</span>

<span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> <span class="hljs-title function_">fetchData</span>(<span class="hljs-params"></span>) { <span class="hljs-keyword">try</span> { <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> <span class="hljs-title function_">fetch</span>(<span class="hljs-string">'https://jsonplaceholder.typicode.com/posts/1'</span>); <span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> response.<span class="hljs-title function_">json</span>(); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(data); } <span class="hljs-keyword">catch</span> (error) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(error); } }

<span class="hljs-title function_">fetchData</span>();</pre></div><h2 id="3822">3. Experimental Flag for CommonJS to ES Modules</h2><p id="7a53">Migrat

Options

ing 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:<code>--cjs-to-esmimportexport"type": "module"package.json</code></p><p id="e966">Before (CommonJS):</p><div id="09e3"><pre><span class="hljs-keyword">const</span> foo = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./foo'</span>); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(foo);</pre></div><p id="5794">After (ES Modules with flag):<code>--cjs-to-esm</code></p><div id="f8c6"><pre><span class="hljs-keyword">import</span> foo <span class="hljs-keyword">from</span> <span class="hljs-string">'./foo'</span>; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(foo);</pre></div><p id="082c">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.</p><p id="c594">These are just some of the exciting changes that Node.js 21.0.0 has to offer. You can read the full changelog <a href="https://nodejs.org/en/blog/release/v21.0.0/">here</a>. If you want to try out this version, you can download it from the official website <a href="https://nodejs.org/en/download/current/">here</a>. Happy coding!</p><figure id="5b97"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*IzVr2ZT1yVTDE52QRTbfAA.png"><figcaption></figcaption></figure></article></body>

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: 10

2. 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!

Nodejs
Programming
JavaScript
Typescript
Node Js Tutorial
Recommended from ReadMedium