a simple, lightweight and zero-dependency package to populate process.env
using a .env
file
you can install the package running the following command:
# for npm
npm i dotenv-reader
# for yarn
yarn add dotenv-reader
At the start of your main file require and init the module
require("dotenv-reader").init();
You must create a .env
file in the root folder of your project, then, add a key and a value, ONLY ONE PER LINE, in this format: KEY=VALUE
.
For example:
PORT=3000
DB_HOST=localhost
DB_USERNAME="main"
you can use or not the quotation marks.
Now you can just use process.env.key
like this.
console.log(process.env.PORT);
console.log(process.env.DB_HOST);
console.log(process.env.DB_USERNAME);
// Output 3000, localhost, main
init
will search and read your .env
file, then parse it and populate it to process.env
, and return and object with an error
key if failed.
const result = der.init();
if (result.error) throw result.error;
Default: path.resolve(process.cwd(), '.env')
you can specify a custom path if your .env
file is not in the root folder of the project
The encoding for the .env
file, by default is "utf-8"
Default: False
You can turn on logging to help debug
Default: False
You can turn on logging to help debug