Out of the lab: Node.js 12 features graduate from experimental status

Nick Anderegg
Developer Relations Engineer
14 Jan 2020

The latest version of Node.js is now available on Platform.sh, and this LTS release delivers many performance optimizations and additional features. Beyond updating the V8 engine to version 7.4, the experimental flag on ECMAScript Modules has been dropped, and Worker Threads have major improvements.

To get your current applications up and running with Node 12 right now, just swap out “nodejs:10” with “nodejs:12” in your .platform.app.yaml file:

    type: "nodejs:12"

Commit this change to a new branch in your repository, push it to your project on Platform.sh, and within a few moments, you’ll be able to test out how your application runs on Node.js 12 in an isolated development environment. Once you’re sure your application is ready for the upgrade, merge your development branch into master, and your updated application will be ready for the world to see.

What’s new in Node.js 12?

This Node.js long-term support (LTS) release primarily brings performance improvements and optimizations, as well as some features designed to make it easier for you to build and debug your applications.

Native ECMAScript Modules

With this LTS release, ECMAScript Modules enter Phase 3 of their development and the --experimental-modules flag is no longer needed to use them. The improvements made to this feature now mean that Node.js applications can support default, named, and namespace exports in import statements referencing ES module files—without needing to be bundled with a tool like webpack.

// Default exports
import module from 'module';

// Named exports
import { foo } from 'module';
import { foo, bar} from 'module';

// Namespace exports
import * as module from 'module';
import { foobar as alias } from 'module';

This ECMAScript Modules feature also means that modules can be dynamically imported with an import() expression:

// Dynamics import return a Promise
var module_promise = import('module');

These improved features were included behind the --experimental-modules flag in the initial release of Node.js 12, and this flag is being dropped as Node.js 12 reaches LTS status.

Improvements to Worker Threads

Another feature losing its experimental flag and graduating to the next level of stability is Worker Threads. With this feature, CPU-intensive JavaScript operations can be offloaded from the main event loop thread to a pool of memory-sharing workers to implement a form of lightweight parallelism. This approach has a benefit over the child_process and cluster modules by supporting process parallelization without requiring each thread to have its own isolated memory.

Lower maintenance burden for native modules

Node.js modules written in C/C++ code—modules that allow your application to interact with the underlying system at a low level—have always had a higher maintenance burden than pure-JavaScript modules. In previous versions of Node.js, native modules only worked with the version for which they were written, and module maintainers had to produce builds for each version of Node.js they wished to support. Application developers using native modules had to take special care to ensure that native modules were loaded and unloaded safely and that imported modules were used in a thread-safe manner.

The introduction of N-API allows module maintainers to produce native builds that work with all subsequent versions of Node.js in addition to the versions of Node.js with which they were built. The abstraction of the N-API provides module maintainers an interface that’s independent from the underlying JavaScript engine implementation and eliminates the burden that traditionally came with maintaining a native module between versions of Node.js.

Modules implementing the content-aware addons feature can be loaded multiple times in multiple contexts, safely unloaded, and safely used in a multithreaded execution environment.

Overall, these new features mean that native add-ons now have a maintenance burden more on par with regular JavaScript modules.

Optimizations, optimizations, optimizations

Node.js 12 doesn’t just come with new features; this LTS release abounds with optimizations. Among other performance improvements, Node.js 12 now caches code for built-in libraries at build time to improve startup time. And the JavaScript heap size is now set based on available resources, rather than defaulting to browser-optimized defaults set by V8. If you’re ready to get started with this update, check out the Node.js blog for all of the Node.js 12 changelogs.