dotenv-parse

Zero dependency .env to object parser

Usage no npm install needed!

<script type="module">
  import dotenvParse from 'https://cdn.skypack.dev/dotenv-parse';
</script>

README

Dotenv Parse

A zero dependency library to parse .env config after loading it using dotenv

Install | Usage

Installation

The library is available on npm

# npm install
npm install --save dotenv-parse
# yarn install
yarn add dotenv-parse

Usage

Let's say we have the following .env file:

FOO=bar
NUM=2
BOOL=false
ARR1=some,thing,that,goes,wow
# we use an asterisk here to turn off the parsing for this variable, it will be returned as a string
BLEEP=false*
# we use an asterisk in the array to turn off parsing for an array value
ARR2=ping,true*,2,100
# a string between backtick won't be parsed
STR=`some,thing,that,goes,wow`

Example usage with dotenv

import dotenv from 'dotenv';
import { parse } from 'dotenv-parse');

let env = dotenv.config({})
if (env.error) throw env.error;
env = parse(env.parsed);

console.log(env);

This will result in the following object:

{
  // String
  FOO: 'bar',
  // Number
  NUM: 2,
  // Boolean
  BOOL: false,
  // Array
  ARR1: [ 'some', 'thing', 'that', 'goes', 'wow' ],
  // NOTE: this was not parsed due to the * asterisk override above
  BLEEP: 'false',
  // NOTE: only the "true*" above was opted out through the use of an asterisk
  ARR2: [ 'ping', 'true', 2, 100 ],
  // NOTE: this was not parsed because the string was between backtick
  STR: 'some,thing,that,goes,wow'
}

Options

A second argument can be provided to dotenvParseVariables with an object of options.

The defaults are listed below:

  • assignToProcessEnv (Boolean) - defaults to true, whether or not to assign the parsed values to process.env
  • overrideProcessEnv (Boolean) - defaults to false, whether or not to override existing values in process.env