What's New in Node.js v23: Key Features and Updates

Node.js v23 is here! Released on October 16, 2024, this version introduces several exciting updates and enhancements. From enabling require(esm) by default to improved test runner capabilities, Node.js v23 is a significant milestone for developers. Let’s dive into what’s new and how it can impact your projects.

Highlights of Node.js v23

  1. require(esm) Enabled by Default
  2. Dropping Support for Windows 32-bit Systems
  3. Stabilization of the node --run Command
  4. Test Runner Enhancements

Node.js v23 will serve as the Current release line until April 2025, replacing Node.js 22, which transitions to Long-Term Support (LTS) later this month.

require(esm) Enabled by Default

One of the most talked-about features in Node.js v23 is the native support for loading ES modules using require(), a feature previously hidden behind the --experimental-require-module flag.

What This Means for Developers

  • You can now use require() to load ES modules without encountering the ERR_REQUIRE_ESM error.
  • However, if the module or its dependencies contain top-level await, Node.js will throw ERR_REQUIRE_ASYNC_MODULE.

Code Example: Loading an ES Module with require()

Here’s how you can use require() to load an ES module:

// module.mjs
export const greet = () => "Hello from ES Module!";

// main.js
const { greet } = require("./module.mjs"); // No ERR_REQUIRE_ESM error
console.log(greet()); // Output: Hello from ES Module!

To check if this feature is enabled in your Node.js instance:

console.log(process.features.require_module); // true if enabled

Note: This feature remains experimental. Use the flag --no-experimental-require-module to disable it if necessary.

Stabilization of the node --run Command

The node --run command, introduced in earlier versions, has been stabilized in Node.js v23. This command simplifies running Node.js scripts directly from the command line.

Example: Running a Script with node --run

node --run "console.log('Node.js v23 rocks!')"

This enhancement improves the developer experience by making script execution faster and more intuitive.

Test Runner Enhancements

Node.js v23 brings powerful updates to its test runner, focusing on glob pattern support for coverage files. This means you can now include or exclude test files using flexible patterns.

Example: Using Glob Patterns in the Test Runner

node --test --coverage "tests/**/*.test.js"

This feature streamlines managing large test suites by allowing developers to specify files with greater precision.

Dropping Support for Windows 32-bit Systems

Starting with Node.js v23, support for Windows 32-bit systems has been officially removed. Developers using these systems are encouraged to transition to 64-bit environments for better performance and compatibility.

Transitioning from Node.js 22 to Node.js 23

If you're upgrading from Node.js 22, here are some tips:

  1. Test Your ES Modules

Ensure your ES modules work seamlessly with require(). Use util.isModuleNamespaceObject() to verify if the imported object is a module namespace.

const util = require("util");
const moduleObj = require("./module.mjs");

console.log(util.isModuleNamespaceObject(moduleObj)); // true
  1. Check System Compatibility
    If you're on a Windows 32-bit system, consider upgrading to 64-bit.

  2. Leverage the New Test Runner Features
    Simplify your testing workflows with glob patterns.

What's Next for Node.js?

Node.js v23 is the Current release until April 2025, when it will transition to LTS. During this time, users are encouraged to provide feedback on experimental features like require(esm).

Conclusion

Node.js v23 marks a significant leap forward with its enhanced module loading capabilities, improved test runner, and streamlined command-line tools. While it introduces cutting-edge features, it's also a good reminder to test your applications thoroughly when upgrading.

Stay tuned for more updates as the Node.js community continues to innovate!

Have questions or feedback about Node.js v23? Let us know in the comments below or join the Node.js GitHub discussions.

Enjoyed this article?

Share it with your network to help others discover it

Continue Learning

Discover more articles on similar topics