src: add config file support by marco-ippolito · Pull Request #57016 · nodejs/node
Refs: #53787
Introducing node.json
node --experimental-config-file=noderc.json file.js
Why a config file?
With the introduction of test runner, sea, and other feature that require a lot of flags, a json config flag would improve by a lot the developer experience and increase adoption.
Example:
node --experimental-config-file=noderc.json --test --experimental-test-coverage
{
"test-coverage-lines": 80,
"test-coverage-branches": 60
}Current solutions
- CLI Flags
- NODE_OPTIONS
--env-filewith NODE_OPTIONS
All of these solutions dont have a good developer experience.
Why JSON?
Simply because we already support json parsing (simdjson) and its kinda the default in the js ecosystem.
Do we have to support all the configurations possible?
NO
Not all flag need to be supported by the config file, and not all flags CAN be supported.
The config file will only support flags that can be passed through NODE_OPTIONS:
https://nodejs.org/api/cli.html#node_optionsoptions
How does it work?
This is an early development, it work similarly to the --env-file flag. It will parse the provided file with --experimental-config-file and add to NODE_OPTIONS the recognized flags. Unrecognized flags will be ignored.
Versioning
json_schema!
When we release a new version, we also release a json schema file.
{
"$schema": "https://nodejs.org/dist/v20.18.3/docs/node_config_json_schema.json",
}The users can has autocompletion and validation of their config file specific to the version they are using.
Since the config file mirrors the features that the current version has, it would be impossible to provide semver major backwards compatibility (immagine a feature gets removed we also have to remove it from the config file).
Examples in the ecosystem
Security
Just like .env files, Node.js entrusts the user to provide a secure and valid configuration.
@nodejs/tsc for visibility since its a big feature