dotenv-reader
TypeScript icon, indicating that this package has built-in type declarations

1.0.1-b • Public • Published

DotEnv-Reader - D.E.R.

a simple, lightweight and zero-dependency package to populate process.env using a .env file

NPM version LICENSE

How to install

you can install the package running the following command:

# for npm
npm i dotenv-reader

# for yarn
yarn add dotenv-reader

How to use

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

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;

Params

pathLike

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

encoding

The encoding for the .env file, by default is "utf-8"

logging

Default: False You can turn on logging to help debug

CastToObj

params

logging

Default: False You can turn on logging to help debug

Package Sidebar

Install

npm i dotenv-reader

Weekly Downloads

0

Version

1.0.1-b

License

MIT

Unpacked Size

6.42 kB

Total Files

6

Last publish

Collaborators

  • kovac.dr