https://github.com/ju1ius/pointenv
Polyglot dotenv parser and evaluator for NodeJS
https://github.com/ju1ius/pointenv
dotenv dotenv-parser nodejs posix shell
Last synced: 17 days ago
JSON representation
Polyglot dotenv parser and evaluator for NodeJS
- Host: GitHub
- URL: https://github.com/ju1ius/pointenv
- Owner: ju1ius
- License: mit
- Created: 2023-02-22T08:42:47.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-16T17:49:47.000Z (about 2 years ago)
- Last Synced: 2025-10-18T07:32:01.845Z (7 months ago)
- Topics: dotenv, dotenv-parser, nodejs, posix, shell
- Language: TypeScript
- Homepage:
- Size: 341 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @ju1ius/pointenv
[](https://app.codecov.io/gh/ju1ius/pointenv)
[](https://www.npmjs.com/package/@ju1ius/pointenv)
[](https://deno.land/x/pointenv)
Polyglot dotenv parser and evaluator.
## Installation
```sh
npm install @ju1ius/pointenv
```
## Supported dialects
* [posix](https://github.com/php-xdg/dotenv-spec)
* [docker-compose](https://docs.docker.com/compose/environment-variables/env-file/)
* [symfony/dotenv](https://github.com/symfony/dotenv)
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` object containing the variables
that have been injected into the environment.
```ts
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`:
```ts
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.
```sh
# .env
BAR="${FOO:-not found}"
```
```ts
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:
```ts
import {load} from '@ju1ius/pointenv'
const env = await load(['.env'])
// env is a Map 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:
```ts
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})
```