Skip to content

Polyglot dotenv parser and evaluator for NodeJS

License

Notifications You must be signed in to change notification settings

ju1ius/pointenv

Repository files navigation

@ju1ius/pointenv

codecov npm deno.land

Polyglot dotenv parser and evaluator.

Installation

npm install @ju1ius/pointenv

Supported dialects

The formal dotenv syntax for this project is posix only.

The posix dialect is a subset of the POSIX shell syntax and is compatible with shell scripts.

Support for other dotenv syntax dialects is included for interoperability purposes. Compatibility will be improved gradually, but 100% compatibility is not always possible, nor desirable (for example symfony supports shell command evaluation, which we don't for obvious reasons).

Usage

The default entrypoint for this module parses and evaluates the given files in order, then injects the resulting variables into the global environment object (process.env or Deno.env).

It returns a Map<string, string> object containing the variables that have been injected into the environment.

import pointenv from '@ju1ius/pointenv'

const applied = await pointenv(['.env', '.env.local'])
console.log(applied)

Variables that are already present in the environment have precedence over those specified in the dotenv files, unless the override option is true:

await pointenv(['.env'], {override: true})

If an env option is provided, the variable resolution will use that instead of the global envionment.

This can be used i.e. for providing defaults for when a variable is not set in the environment.

# .env
BAR="${FOO:-not found}"
const env = await pointenv(['.env'], {
  env: {
    FOO: 'bar',
    ...process.env,
  }
})
console.log(env.get('BAR')) // 'bar'

If you just want to parse and evaluate the files without injecting anything into the environment, use the load function:

import {load} from '@ju1ius/pointenv'

const env = await load(['.env'])
// env is a Map<string, string> containing all the variables
// found in the provided files
console.log(env)

Using alternative dialects

The dialect option can be set to one of the supported alternative dialects:

import pointenv, {load, Dialect} from '@ju1ius/pointenv'
// use the docker-compose dialect
await pointenv(['.env'], {dialect: Dialect.Compose})
// use the symfony dialect
await load(['.env'], {dialect: Dialect.Symfony})