Note: JSR is frozen at 4.5.x. From 5.0.0,
@eserstack/configis published on npm under the same name (pnpm add @eserstack/config); the JSR versions stay available but get no updates.
eserstack Library — eser/stack on GitHub Install:
pnpm add @eserstack/config
@eserstack/config helps you load configurations from .env.* files and
environment variables, a practice based on The 12-Factor App methodology
which recommends separating configuration from code.
Environment variables (often shortened to env vars or env) are variables available in all command line sessions and affect the behavior of the applications on your system. They are essential for managing the configuration of your applications separate from your code.
The 12-Factor App methodology recommends storing configurations in environment variables.
The 12-Factor App is a set of best practices designed to enable applications to be built with portability and resilience when deployed to the web. Learn more about The 12-Factor App.
@eserstack/config helps you handle these configurations properly. With using
@eserstack/config, the environment variables are loaded from the following
sources:
- Environment variables passed to the Node.js process.
- Environment variables defined by the system's shell.
.env.$(ENV).localfile - Local overrides of environment-specific settings..env.localfile - Local overrides. This file is loaded for all environments except "test"..env.$(ENV)file - Environment-specific settings..envfile - The default configuration file.
These files are loaded in the order listed above. The first value set (either
from file or environment variable) takes precedence. That means you can use the
.env file to store default values for all environments and
.env.development.local to override them for development.
Here you'll find a list of features provided by @eserstack/config along with
brief descriptions and usage examples.
The .env loader lives in the @eserstack/config/dotenv entry point. load()
returns a Map of variable names to values.
Basic usage:
import * as dotenv from "@eserstack/config/dotenv";
const vars = await dotenv.load();
console.log(vars.get("PORT"));Load from different directory:
import * as dotenv from "@eserstack/config/dotenv";
const vars = await dotenv.load({ baseDir: "./config" });
console.log(vars.get("PORT"));The .env parser and loader (load(), parseEnvFromFile(),
parseEnvString()) and the configuration file loader of
@eserstack/config/file (locate(), getFileFormat(), parse(), load())
run in Go through the @eserstack/ajan bridge (the native library, or its WASM
build). The synchronous parseEnvString() and getFileFormat() load the
bridge on first use, and its WASM build compiles on the calling thread. When
neither loads, they throw, and the async functions reject, with the bridge's
load error. On the WASM build, files are read only under the working
directory.
Basic usage:
import * as dotenv from "@eserstack/config/dotenv";
const options = await dotenv.configure<Record<string, unknown>>(
(reader, acc) => {
acc["env"] = reader.getCurrentEnv();
acc["port"] = reader.readInt("PORT", 8080);
return acc;
},
{},
);With custom interfaces:
import * as dotenv from "@eserstack/config/dotenv";
type Options = {
env: string;
port: number;
};
const options = await dotenv.configure<Options>((reader, acc) => {
acc.env = reader.getCurrentEnv();
acc.port = reader.readInt("PORT", 8080);
return acc;
}, {});These changes since 4.5.2 need a change in code, commands or files that use this package. The changelog lists the changes in every package.
- dotenv
load(),configure()andparseEnvFromFile()read values as written instead of runningdecodeURIComponentover them, and a lone%no longer throws. Before:NAME=hello%20world. After:NAME="hello world". - dotenv
load()takes the environment name from the value of the first set, non-empty variable inenvVars(ENV,APP_ENV,NODE_ENVby default) and honoursoptions.envVars. It used to use the variable's name, soENV=productionloaded.env.ENV. Rename such a file after the value:.env.ENVbecomes.env.production. DENO_ENVno longer names the environment. SetENV,APP_ENVorNODE_ENVinstead, or passenvVars: ["DENO_ENV"]toload().- dotenv parsing expands a reference to a name that neither the file nor the
process sets, with no default, to an empty string. Before:
A=x${UNSET}readxundefined. After: it readsx. - file
parse()andload()word JSON and JSONC syntax errors as Go does, with the line and byte column. Before:Cannot parse JSONC: CommaExpected at line 3, column 3. After:syntax error: line 3, column 3: invalid character '"' after object value (expecting ',' or '}'). The error is still aConfigParseErrorwith aSyntaxErrorcause; match on the class, not the text.
🔗 For further details, visit the eserstack repository.