CLI
Node.js --env-file-if-exists
Loads an optional environment file without failing solely because it is absent. Required application configuration must still be validated. Tested on Node.js 22.16.0. Example output (one line per entry): ["default"].
Formal Syntax & Structure
nodejs syntaxnodejs
node --env-file-if-exists=config.env app.mjsParameters & Attributes
| Name | Type | Description |
|---|---|---|
| config.env | path | Optional configuration file. |
Return Value
Process output
Result depends on the invoked program.
Working Example
Example Snippetnodejs
import { execFileSync } from 'node:child_process';import { writeFileSync, mkdtempSync, rmSync } from 'node:fs';import { join } from 'node:path';import { tmpdir } from 'node:os';const dir = mkdtempSync(join(tmpdir(), 'bytemind-'));try { const result = execFileSync(process.execPath, ["--env-file-if-exists=absent.env", "-e", "console.log(process.env.LAB_MODE??'default')"], { cwd: dir, env: { PATH: process.env.PATH, HOME: dir, ...{} }, encoding: 'utf8', timeout: 3000, stdio: ['ignore', 'pipe', 'pipe'] }); console.log(result.trim());}finally { rmSync(dir, { recursive: true, force: true });}