Skip to content

Instantly share code, notes, and snippets.

@BananaAcid
Last active April 10, 2025 20:29
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 _
const rcEnvFilePath = path.join(os.homedir(), '.progrc');
(await readFile(rcEnvFilePath, '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(line => line[1].length > 0).forEach(line => process.env[line[0].toUpperCase().replaceAll(' ', '_')] = line[1]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment