Skip to content

Instantly share code, notes, and snippets.

@BananaAcid
Last active May 5, 2025 16:25
Show Gist options
  • Save BananaAcid/3917838afa9dd31677755cf44c0c1cb8 to your computer and use it in GitHub Desktop.
Save BananaAcid/3917838afa9dd31677755cf44c0c1cb8 to your computer and use it in GitHub Desktop.
JavaScript: node:parseEnv sucks really badly.

JavaScript: node:parseEnv sucks really badly.

This is my version.

Loads all ENV vars to the process.env

  • ignore comments (any amount of spaces then #)
  • any = after the first is part of the value
  • keys will always be uppercase and spaces replaced with _
(await readFile(RC_ENVFILE, 'utf-8').catch(_ => ''))
.split('\n').map(line => line.trim()).filter(line => line.length > 0 && !line.startsWith('#'))
.map(line => line.split(/=(.*)/).map(part => part.trim()) )
.filter(([key,val]) => key && val)
.forEach(([key,val]) => process.env[key!.toUpperCase().replaceAll(' ', '_')] = val);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment