New:Microsoft Teams Notifications Are Now Available in Socket.Learn more
Get Started

devkits-env-parser

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

devkits-env-parser

Parse .env file content into JavaScript object — zero dependencies

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

devkits-env-parser

Parse .env file content into JavaScript object. Zero dependencies, handles comments, quotes, and edge cases.

Install

npm install -g devkits-env-parser

npm version

Usage

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.

ParamTypeDescription
contentstring.env file content
ReturnsObjectParsed 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.

ParamTypeDescription
objObjectKey-value pairs
optionsObjectOptions
options.quotebooleanQuote all values (default: false)
options.commentsObjectAdd comments to keys
Returnsstring.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
  • dotenv — Load .env into process.env
  • env-cmd — Run commands with environment variables

See Also

Support

Open Collective: Open Collective - DevKits

Crypto: ETH/USDC 0xDea4a6A20fCB44467e45Ef378972F54B22dC59db

License

MIT — DevKits Team

Keywords

devkits

FAQs

Package last updated on 12 Mar 2026

Related posts