@nodesuite/env
v0.3.32
Published
Dotenv loader with recursive search.
Downloads
26
Maintainers
Readme
@nodesuite/env
Simple environment .env
file parser with recursive search.
Background
In nearly every case, I would suggest using the excellent dotenv
family of libraries as they are well maintained
and better supported:
Primary Use-Cases
The primary reasons for creating this package are the following scenarios where dotenv
loses contact with the
location of the intended .env
files:
- When bundling and packaging a Node application as a binary, as the final executable may be moved around the end-user's filesystem in unpredictable locations.
- When working within a monorepo,
dotenv
is sometimes unable to locate the correct file when deeply nested.
Although the dotenv
family of libraries allow specifying a location of the file, this package handles locating the
file when the location is unknown.
Importantly, the primary concern is loading .env
files into process.env
as opposed to acting as a full
configuration manager.
It can be used prior to initialization of a larger configuration package such as
mozilla/node-convict or nconf in
edge-cases where .env
files cannot be located by the main library.
Additional Use-Cases
Additional use-cases this package handles are:
- Passing a specific
.env
file path for an arbitrary location to a child process using explicit environment variables. - When called manually, allows filtering of sensitive keys with string or regex pattern.
Usage
Inline
Example usage alongside convict
:
import "@nodesuite/env"
import convict from "convict"
const config = convict({
env: {
doc: "The application environment (always in process.env).",
format: ["development", "test", "production"],
default: "development",
env: "NODE_ENV"
},
foo: {
doc: "Example variable (loaded from .env file).",
format: Boolean,
default: false,
env: "FOO"
}
})
Manual
import { load , type Env} from "@nodesuite/env"
interface MyEnv extends Env {
MY_SAFE_VALUE?: string
MY_UNSAFE_VALUE: never
RANDOM_API_KEY: never
}
const { env } = load({
filters: ["MY_UNSAFE_VALUE", /API_KEY/g]
})