Highlights of Node.js v23
require(esm)
Enabled by Default- Dropping Support for Windows 32-bit Systems
- Stabilization of the
node --run
Command - 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 theERR_REQUIRE_ESM
error. - However, if the module or its dependencies contain
top-level await
, Node.js will throwERR_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:
- 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
-
Check System Compatibility
If you're on a Windows 32-bit system, consider upgrading to 64-bit. -
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.