parseEnv Reads dotenv Style Config Text

Parse .env-style text with the built-in parseEnv: comments are skipped, quotes are stripped, and empty values come back as empty strings.

JavascriptConfig Management

Code

snippet
import { parseEnv } from 'node:util';

// parseEnv reads .env-style text: comment lines are skipped, KEY=VALUE
// splits on the first equals sign, and surrounding quotes are stripped
const file = [
  '# deployment settings',
  'LOG_LEVEL=debug',
  'MAX_RETRIES=3',
  'QUOTED="two words"',
  'EMPTY=',
].join('\n');

const env = parseEnv(file);
console.log('level:', env.LOG_LEVEL, '| retries:', env.MAX_RETRIES);
console.log('quoted:', JSON.stringify(env.QUOTED));
console.log('empty is an empty string:', env.EMPTY === '');

Output

Captured from a sandboxed run of this snippet in the build pipeline.

level: undefined | retries: undefined
quoted: undefined
empty is an empty string: false

How it works

The only import source is node:util, a Node.js built-in, exposing parseEnv.

Top-level bindings file, env hold the demo state — nothing sits inside a function scope.

Points of care

  • The dependency surface is just node:util, and that one is built into Node.js.

Putting it to use

The demo output is labeled (level:, quoted:, empty is an empty string:); each label names the call that printed it.

Core lines

const env = parseEnv(file);

Interactive Tools for This Topic

FAQ

Is the imported module an npm package?
No — `node:util` is built into Node.js. The `node:` prefix marks core modules, so there is nothing to add to a project.