Node.js 21: A Powerful Upgrade In Node JS
Introduction
Node.js 21 was released on October 17, 2023 and came with a number of new features and improvements. This includes experimental support for the WebSocket API, a new –experimental-default-type flag, and a number of updates to the test runner and llhttp parser.
Experimental support for WebSocket API
Node.js 21 introduces experimental support for the WebSocket API, which allows you to connect to a WebSocket server from a Node.js application without resorting to third-party packages. This is an important innovation, as it allows real-time, two-way interactive communication between the client and server.
const WebSocket = require('ws'); const ws = new WebSocket('ws://localhost:8080'); ws.on('open', () => { console.log('WebSocket connection open'); }); ws.on('message', (message) => { console.log('Received message:', message); }); ws.on('close', () => { console.log('WebSocket connection closed'); });
New –experimental-default-type flag
By default, Node.js will interpret any code you enter or send it as an ES module if you do not specify otherwise, and if the package.json file is not present. The new –experimental-default-type flag allows you to override this behavior and specify that code should be interpreted as CommonJS instead.
To use the –experimental-default-type flag, you can use the following command:
node --experimental-default-type=commonjs my-script.js
This will cause the my-script.js file to be interpreted as CommonJS, even if it does not have an .extensions file extension.
Test runner concurrency flag
Node.js 21 introduces a new –test-concurrency flag that allows you to specify the number of parallel processes that the test runner should use. This can be useful for improving performance on machines with multiple cores.
To use the –test-concurrency flag, you can use the following command:
node --test-concurrency=4 test
This will cause the test runner to execute tests in parallel using four processes.
Test runner now supports passing globs
Node.js 21 now supports passing globs as arguments to the test runner. This allows you to specify which test files to run based on patterns, rather than having to specify each file individually.
To run all test files that match the *.test.js pattern, you can use the following command:
node test *.test.js
Update on llhttp Version 9.1.2
Node.js 21 uses llhttp version 9.1.2, which introduces a number of new features and bug fixes. One of the most important changes is that llhttp 9.1.2 now has a strict mode that is enabled by default. This means that llhttp will now reject requests that are not formatted correctly.
If you are using a third-party library that sends malformed requests, you may need to disable strict mode in order to use it. To do this, you can use the following command:
node --insecure-http-parser my-script.js
Conclusion
Node.js 21 is a significant release that includes a number of new features and improvements. The experimental support for the WebSocket API is particularly exciting, as it opens up new possibilities for real-time applications.
For more information, please head over to our Hire Node Developer page and to develop a web application using Node JS, Hire Node Developer at HK Infosoft – we are destined to provide you with an innovative solution using the latest technology stacks. E-mail us any clock at – hello@hkinfosoft.com or Skype us: “hkinfosoft”.
To develop Android Mobile Apps, please visit our technology page.
Content Source:
- Node JS Official Documentation