Node.js 20.6 supports .env configuration files and adds C++ garbage collection library Oilpan

In the latest version 20.6.0 , Node.js now has built-in .envsupport for files. You can now .envload environment variables from a file into process.envyour Node.js application, completely dependency-free.

Loading .envfiles is now very simple:

node --env-file .env

What is .env?

.envFile is used to configure environment variables that will exist in the running application. The idea comes from the twelve-factor application approach , which says to store everything in your environment that may differ between deployments (e.g. development, staging, production) .

Configuration should not be part of the application code, nor should it be checked into version control. Things like API credentials or other secrets should be stored separately and loaded into the environment that requires them. Files .envallow you to manage the configuration of applications (such as development machines or CI) where setting variables in the environment is impractical.

There are libraries in many different languages ​​that support .envloading variables into the environment using files, they are often called "dotenv", and Node.js dotenv is no exception. But now, Node.js natively supports this behavior.

How to use .env in Node.js?

The file .envlooks like this:

PASSWORD=supersecret
API_KEY=84de8263ccad4d3dabba0754e3c68b7a
# .env files can have comments too

By convention you save it .envin the root directory of your project, although you can recall it at will.

You can then set the variables in the file as environment variables by launching Node.js and pointing --env-filethe flag to your .envfile. When loading, variables can be used as attributes process.env.

$ node --env-file .env
Welcome to Node.js v20.6.0.
Type ".help" for more information.
> console.log(process.env.PASSWORD)
supersecret
undefined
> console.log(process.env.API_KEY)
84de8263ccad4d3dabba0754e3c68b7a
undefined

Supported features

Compared to dotenv , the support is pretty basic now. For example:

  • Multi-line values ​​are not currently available
  • Cannot use variable expansion
  • Only one file can be specified at a time. Node.js will only use the last flag passed, so in the following example, only the from .env.developmentvariable is used:
node --env-file .env --env-file .env.development

There is more work to be done and some of these features may be added. You can follow the discussion on GitHub here .

shortcoming

As of version 20.6.0, the documentation states , "If the same variable is defined in the environment and the file, the value in the environment takes precedence. This is the default way all dotenv packages work. However, this is not the case with the current implementation of Node.js , variables in the file .envwill override the environment. There is a PR request to correct this.

Benefits of Node.js implementation

Although this implementation lacks some features, it has some advantages over using third-party packages. Node.js loads and parses the file when it starts .env, so you can include environment variables that configure Node itself, for example NODE_OPTIONS.

So you could have a .envfile like this:

NODE_OPTIONS="--no-warnings --inspect=127.0.0.1:9229"

Then, when you run node --env-file=.env, the process will run without warning and it will activate 127.0.0.1:9229the checker on the IP address.

NOTE: You are not allowed to put in NODE_OPTIONS="--env-file .env". Avoid infinite loops.

C++ garbage collection library Oilpan

In addition, the Node.js C++ extension suite now supports Oilpan, the C++ garbage collection library in V8, and provides developers with new tools and guidance to better integrate and manage objects and resources between C++ and JavaScript. In other words, after 20.6, Node.js adds a novel and more efficient C++ memory management method, but the original method is still available.

The V8 team released Oilpan in 2021, which is characterized by providing a tracking garbage collection mechanism that can identify live objects by marking objects, and recycle dead objects during the recycling phase. Oilpan also supports heap fragmentation of certain objects. Collation (Compaction) function. Overall, Oilpan simplifies the program development model and helps solve the problems of memory leaks and release after use.

Developers who write C++ extension kits for Node.js applications can also use Oilpan, but the official reminder is that Node.js only packages the Oilpan function library from V8, and they cannot guarantee the stability of the ABI (Application Binary Interface). But because Oilpan has been tested in Chromium for many years, they also believe that it is unlikely to break the ABI. Later, when Oilpan has enough external and internal adoption, officials will consider including it in the ABI stability guarantee.

Node.js 20.6 also adds a new API to its module system, providing new methods for developers to specify files and customize module Hooks. This new feature provides a flexible and powerful method that allows developers to customize Module loading and parsing behavior, and establishing communication channels between the main application execution thread and the custom Hook execution thread.

Node.js is constantly improving

Go try Node.js version 20.6.0! Version 20 brings new features such as a stable test runner, simulation timers and now .envfile support, as well as many other upgrades, fixes and improvements. Version 20 becomes the active LTS version of Node.js in October, so now is a good time to test out these new features and start thinking about upgrading your application to take advantage.

Guess you like

Origin blog.csdn.net/jslygwx/article/details/132732621