Node.js 20.6 has been released, the latest update to this popular JavaScript runtime, with new features including the ability to configure environment variables in a file passed to the application on launch, and experimental support for garbage-collected C++ via a V8 project called Oilpan. V8 is the JavaScript and WebAssembly engine also used by Google Chrome, Deno, and many other projects.
The idea of support for .env files is that developers can initialize a Node.js application with a predefined configuration using a command such as:
node --env-file=config.env index.js
.
A .env file is a collection of key-value pairs in the INI file format. These files allow both environment variables and Node options to be defined. Setting Node options is done via the NODE_OPTIONS environment variable, which is a space-delimited set of what would otherwise be command-line options.
A popular Node module called dotenv performs a similar function; however this is only available for use in user code after Node has launched, so cannot be used to configure the Node runtime itself.
While this seems a useful feature, it raises some tricky issues. What if the same variable is defined both as an actual environment variable, and via a .env file? The answer is that the value from the environment takes precedence, which is better for security. What about support for multiple incremental .env files where one can override another? Such requests caused one commenter to declare the feature “an infinitely slippery slope.” It is noted that features such as multiline values, variable expansion, and incremental .env support are “not implemented in this current context.”
Oilpan is a V8 library which implements a trace-based garbage collector. The V8 team said that garbage collection through reference counting, which was used by the Blink browser engine when it was first forked from WebKit, “is known to be prone to memory leaks due to cycles,” as well as vulnerability to use-after-free issues “as sometimes reference counting would be omitted for performance reasons.” Oilpan was therefore implemented first for Blink, and then separated into a library and hosted in the V8 engine from version 9.4, released in September 2021.
Node.js will now support Oilpan, also known as cppgc, for those writing C++ add-ons for Node applications. The ABI (Application Binary Interface) stability of cppgc is not guaranteed, but the Node team said that “we do not expect the ABI to break often, as it has been stable and battle-tested in Chromium for years.”
There are numerous other features and bug-fixes in Node.js 20.6, and many more going back to the initial release of 20.0.0 in April this year.
Node.js 20 is an even-numbered release which means it will become an LTS (Long Term Support) version, according to the published release schedule. The date set for this is 24 October 2023, approximately one year after Node.js 18 became an LTS release.