devkits-env-parser
v1.0.0
Published
Parse .env file content into JavaScript object — zero dependencies
Maintainers
Readme
devkits-env-parser
Parse .env file content into JavaScript object. Zero dependencies, handles comments, quotes, and edge cases.
Install
npm install -g devkits-env-parserUsage
CLI
# Parse .env content to JSON
env-parser parse "FOO=bar\nBAZ=qux"
# Output: {"FOO":"bar","BAZ":"qux"}
# Parse with quotes
env-parser parse "DB_HOST=\"localhost\"\nPORT=5432"
# Stringify to .env format
env-parser stringify '{"DB_HOST":"localhost","PORT":"5432"}'
# Output: DB_HOST=localhost\nPORT=5432
# Parse .env file
env-parser file .env
# Quote all values
env-parser stringify '{"MSG":"hello world"}' --quote
# Output: MSG="hello world"Programmatic API
const { parse, stringify } = require('devkits-env-parser');
// Parse .env content
const config = parse(`
# Database settings
DB_HOST=localhost
DB_PORT=5432
DB_NAME="my database" # inline comment
`);
// { DB_HOST: 'localhost', DB_PORT: '5432', DB_NAME: 'my database' }
// Parse with quotes
parse('MESSAGE="hello world"');
// { MESSAGE: 'hello world' }
// Handle escaped quotes
parse('QUOTE="say \\"hello\\""');
// { QUOTE: 'say "hello"' }
// Stringify to .env format
stringify({ DB_HOST: 'localhost', PORT: '5432' });
// 'DB_HOST=localhost\nPORT=5432'
// Add comments
stringify(
{ DB_HOST: 'localhost' },
{ comments: { DB_HOST: 'Database host' } }
);
// 'DB_HOST=localhost # Database host'
// Quote all values
stringify({ MSG: 'hello world' }, { quote: true });
// 'MSG="hello world"'API
parse(content)
Parse .env file content into object.
| Param | Type | Description | |-------|------|-------------| | content | string | .env file content | | Returns | Object | Parsed key-value pairs |
Features:
- Skips comments (lines starting with #)
- Handles inline comments (
KEY=value # comment) - Removes surrounding quotes
- Unescapes escaped quotes (
\"→") - Ignores empty lines
stringify(obj, options)
Stringify object into .env format.
| Param | Type | Description | |-------|------|-------------| | obj | Object | Key-value pairs | | options | Object | Options | | options.quote | boolean | Quote all values (default: false) | | options.comments | Object | Add comments to keys | | Returns | string | .env file content |
Use Cases
- Config loading — Parse .env files in Node.js apps
- Config validation — Check .env file structure
- Migration scripts — Convert between config formats
- CI/CD — Generate .env files from secrets
- Debugging — Inspect environment configurations
Examples
// Load and parse .env file
const fs = require('fs');
const { parse } = require('devkits-env-parser');
const content = fs.readFileSync('.env', 'utf8');
const config = parse(content);
// Generate .env from template
const { stringify } = require('devkits-env-parser');
const envContent = stringify({
NODE_ENV: 'production',
PORT: '3000',
API_KEY: process.env.API_KEY
}, {
comments: {
NODE_ENV: 'Environment',
PORT: 'Server port'
}
});Features
- Zero dependencies — Pure JavaScript
- Comment support — # comments and inline comments
- Quote handling — Single and double quotes
- Escape sequences — Handles
\"and\' - CLI + library — Command line and programmatic use
- Lightweight — ~2KB minified
Related Tools
- dotenv — Load .env into process.env
- env-cmd — Run commands with environment variables
See Also
- DevKits — Developer tools
- @hezelclark/json-formatter — Format JSON
Support
Open Collective: Open Collective - DevKits
Crypto: ETH/USDC 0xDea4a6A20fCB44467e45Ef378972F54B22dC59db
License
MIT — DevKits Team
