From bf7ff369f6116c47d3f814a9439ad3c09a2b4d61 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Thu, 17 Aug 2023 10:08:05 -0400 Subject: [PATCH] src: add built-in `.env` file support PR-URL: https://github.com/nodejs/node/pull/48890 Refs: https://github.com/orgs/nodejs/discussions/44975 Reviewed-By: Geoffrey Booth Reviewed-By: Matteo Collina Reviewed-By: Benjamin Gruenbaum Reviewed-By: Paolo Insogna --- doc/api/cli.md | 36 +++++ node.gyp | 2 + src/env.cc | 9 +- src/env.h | 5 +- src/heap_utils.cc | 4 +- src/inspector_profiler.cc | 6 +- src/node.cc | 26 +++- src/node_credentials.cc | 1 - src/node_dotenv.cc | 164 ++++++++++++++++++++++ src/node_dotenv.h | 38 +++++ src/node_options.cc | 6 + src/node_options.h | 2 + src/node_report.cc | 2 +- test/fixtures/dotenv/node-options.env | 5 + test/fixtures/dotenv/valid.env | 35 +++++ test/parallel/test-dotenv-edge-cases.js | 38 +++++ test/parallel/test-dotenv-node-options.js | 73 ++++++++++ test/parallel/test-dotenv.js | 70 +++++++++ tools/test.py | 2 +- 19 files changed, 508 insertions(+), 16 deletions(-) create mode 100644 src/node_dotenv.cc create mode 100644 src/node_dotenv.h create mode 100644 test/fixtures/dotenv/node-options.env create mode 100644 test/fixtures/dotenv/valid.env create mode 100644 test/parallel/test-dotenv-edge-cases.js create mode 100644 test/parallel/test-dotenv-node-options.js create mode 100644 test/parallel/test-dotenv.js diff --git a/doc/api/cli.md b/doc/api/cli.md index 429ddf4ff32d19..6bf426432aad15 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -980,6 +980,41 @@ surface on other platforms, but the performance impact may be severe. This flag is inherited from V8 and is subject to change upstream. It may disappear in a non-semver-major release. +### `--env-file=config` + +> Stability: 1.1 - Active development + + + +Loads environment variables from a file relative to the current directory, +making them available to applications on `process.env`. The [environment +variables which configure Node.js][environment_variables], such as `NODE_OPTIONS`, +are parsed and applied. If the same variable is defined in the environment and +in the file, the value from the environment takes precedence. + +The format of the file should be one line per key-value pair of environment +variable name and value separated by `=`: + +```text +PORT=3000 +``` + +Any text after a `#` is treated as a comment: + +```text +# This is a comment +PORT=3000 # This is also a comment +``` + +Values can start and end with the following quotes: `\`, `"` or `'`. +They are omitted from the values. + +```text +USERNAME="nodejs" # will result in `nodejs` as the value. +``` + ### `--max-http-header-size=size`