@rav2040/dotenv
A non-mutating Node.js library for loading environment variables with TypeScript support.
Installation
npm install -D @rav2040/dotenv
Usage
With the following .env file in your project root:
FOO = "bar"
Create a new environment object (process.env
will be left unchanged) by importing createEnv
:
import { createEnv } from "@rav2040/dotenv";
const Env = createEnv();
Variables become accessible via the following:
const foo = Env.get("FOO"); // "bar"
const baz = Env.get("BAZ"); // undefined
You can set a variable as being required by passing true
as the second argument:
const foo = Env.get("FOO", true); // "bar"
const baz = Env.get("BAZ", true); // Throws an error
JSON
Returned values will be parsed as JSON by default.
e.g. FOO
is set to 42
:
const foo = Env.get<number>("FOO"); // 42
console.log(typeof foo); // "number"
e.g. FOO
is set to {"hello":"world"}
:
const foo = Env.get<{ hello: string }>("FOO"); // { hello: "world" }
console.log(typeof foo); // "object"
Configuration
const config = {
// ...
};
const Env = createEnv(config);
path?: string
Path of the environment file. If not set AND .env
does not exist in the project root then only variables from process.env
will be loaded. Otherwise variables from process.env
and the environment file will be merged. Defaults to .env
in the project root.
encoding?: BufferEncoding
Buffer encoding of the environment file. Defaults to "utf8"
.
override?: boolean
Variables set in the environment file will take precedence over existing environment variables when there are conflicts. Defaults to false
.
parseJson?: boolean
Values that are valid JSON will be deserialized. Defaults to true
.