From 76ea37d0216affc5da081d091ee5e6309dd60b5d Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 15 Jan 2024 14:28:51 -0500 Subject: [PATCH] src: add `process.loadEnvFile` programmatic API --- doc/api/process.md | 23 +++++++ lib/internal/bootstrap/node.js | 1 + lib/internal/process/per_thread.js | 12 ++++ src/node_process.h | 2 + src/node_process_methods.cc | 22 +++++++ test/fixtures/dotenv/.env | 1 + test/parallel/test-process-load-env-file.js | 72 +++++++++++++++++++++ 7 files changed, 133 insertions(+) create mode 100644 test/fixtures/dotenv/.env create mode 100644 test/parallel/test-process-load-env-file.js diff --git a/doc/api/process.md b/doc/api/process.md index df7af5feaa92f9..587b9c499fd00b 100644 --- a/doc/api/process.md +++ b/doc/api/process.md @@ -2260,6 +2260,29 @@ process.kill(process.pid, 'SIGHUP'); When `SIGUSR1` is received by a Node.js process, Node.js will start the debugger. See [Signal Events][]. +## `process.loadEnvFile(path)` + + + +> Stability: 1.1 - Active development + +* `path` {string | undefined} + +Loads the `.env` file into `process.env`. By default, the path is resolved to +`path.resolve(process.cwd(), '.env')`. If file is not found, no error is thrown. + +```cjs +const { loadEnvFile } = require('node:process'); +loadEnvFile(); +``` + +```mjs +import { loadEnvFile } from 'node:process'; +loadEnvFile(); +``` + ## `process.mainModule`