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.
Code
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: falseHow 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
Dotenv File ParserParse .env file content into a JSON object, handling comments, export prefixes, single quotes, and double-quoted escape sequences.INI File ParserParse INI configuration files into nested JSON with section objects, global keys, and both semicolon and hash comment support.JSON to TOML ConverterTurn a JSON object into TOML, emitting nested objects as tables and arrays of objects as array-of-tables blocks.JSON to YAML ConverterConvert JSON to readable YAML with nested block mappings, sequences, and quoting only where it is required.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.