CLI

Node.js --env-file

Loads environment settings before running the program. An already defined environment variable takes priority. Use synthetic values in examples, not real secrets. Tested on Node.js 22.16.0. Example output (one line per entry): ["host 8080"].

Formal Syntax & Structure

nodejs syntaxnodejs
node --env-file=config.env app.mjs

Parameters & Attributes

NameTypeDescription
config.envpathConfiguration 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 {    writeFileSync(join(dir, 'config.env'), 'MODE=file\nPORT=8080\n');    const result = execFileSync(process.execPath, ["--env-file=config.env", "-e", "console.log(process.env.MODE,process.env.PORT)"], { cwd: dir, env: { PATH: process.env.PATH, HOME: dir, ...{ MODE: 'host' } }, encoding: 'utf8', timeout: 3000, stdio: ['ignore', 'pipe', 'pipe'] });    console.log(result.trim());}finally {    rmSync(dir, { recursive: true, force: true });}
Node.js Reference