2.0.9 • Published 2 years ago

@jsdotenv/core v2.0.9

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

dotenv

Written in typescript, full testing.

It can loads environment variables from a .env file into process.env or parse <key>=<value> string

Installation

npm i @jsdotenv/core

Usage

Load env file

Add your application configuration to your .env file in the root of your project:

S3_BUCKET=YOURS3BUCKET
SECRET_KEY=YOURSECRETKEYGOESHERE

Then in your Nodejs app you can do something like

import dotenv from "@jsdotenv/core";

dotenv.load([__dirname + "/.env"]);
console.log(process.env["S3_BUCKET"]);

If you want to be really fancy with your env file you can do comments and exports (below is a valid env file)

# I am a comment and that is OK
SOME_VAR=someval
FOO=BAR # comments at line end are OK too
export BAR=BAZ

Or finally you can do YAML(ish) style

FOO: bar
BAR: baz

Multiple line is OK.

MULTI_DOUBLE_QUOTED="THIS
IS
A
MULTILINE
STRING"

Expand variables is OK.

OPTION_B=${OPTION_A}
OPTION_C=$OPTION_B
OPTION_D=${OPTION_A}${OPTION_B}
OPTION_E=${OPTION_NOT_DEFINED}

Writing Env Files

dotenv can also write a map representing the environment to a correctly-formatted and escaped file.

const map = new Map();
map.set("BASIC", "basic");
map.set("KEY", "value");
const filepath = path.resolve(__dirname, "./jsfile/.env");
dotenv.write(map, filepath);

... or to a string

const map = new Map();
map.set("BASIC", "basic");
map.set("KEY", "value");
const lines = dotenv.marshal(map);

Exec commands

dotenv can run commands and inherit output.

exec bash commands

const pathname = path.resolve(__dirname + "/env/.env");
const out = dotenv.exec([pathname], "bash", ["-c", 'echo "$BASIC"']);

exec nodejs commands

const pathname = path.resolve(__dirname, "./jsfile/hello.js");
const out = dotenv.exec([], "node", [pathname]);

Cli

npm i create-dotenv -g

Usage

Execute commands using key-value pairs.

dotenv-cli -v KEY=VALUE -- bash -c 'echo "$KEY"'

Execute commands using enviroment file.

dotenv-cli -e .env -- bash -c 'echo "$BASIC"'

Execute commands --help for Usage

dotenv-cli --help
1.1.1

2 years ago

1.1.0

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.1.4

2 years ago

1.0.5

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago

2.0.7

2 years ago

2.0.6

2 years ago

2.0.9

2 years ago

2.0.8

2 years ago

2.0.1

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.4

2 years ago