From 24aba0167a14d1698565dd89593ed17636e9d7e3 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sun, 23 Jul 2023 00:05:25 -0400 Subject: [PATCH] src: add built-in `.env` file support --- doc/api/cli.md | 26 ++++++++ node.gyp | 2 + src/node.cc | 6 ++ src/node_dotenv.cc | 115 +++++++++++++++++++++++++++++++++ src/node_dotenv.h | 21 ++++++ src/node_options.cc | 5 ++ src/node_options.h | 2 + test/fixtures/dotenv/valid.env | 38 +++++++++++ test/parallel/test-dotenv.js | 72 +++++++++++++++++++++ 9 files changed, 287 insertions(+) create mode 100644 src/node_dotenv.cc create mode 100644 src/node_dotenv.h create mode 100644 test/fixtures/dotenv/valid.env create mode 100644 test/parallel/test-dotenv.js diff --git a/doc/api/cli.md b/doc/api/cli.md index cf509503dc838a..de66fccb32d858 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -975,6 +975,32 @@ 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 - Experimental + + + +Loads environment variables from a file relative to the current directory. + +`NODE_OPTIONS` environment variable is not supported at the moment. + +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 +``` + ### `--max-http-header-size=size`