1.2.0 • Published 5 years ago
environment-variable-reader v1.2.0
Environment Variable Reader
Install
npm i environment-variable-readerUsage
Create a file named .env in your root directory. And fill it with content like:
DB_NAME=thedb
DB_HOST=localhostThen in your code you can access DB_NAME like so:
const { getEnvironmentVariable } = require('environment-variable-reader');
const dbName = getEnvironmentVariable('DB_NAME');Default value
If your .env file does not have DB_USER and we want DB_USER to have a default value than we can do it like this:
const dbUser = getEnvironmentVariable('DB_USER', 'root');Otherwise if we don't specify a default value and that environment variable does not not exist, then getEnvironmentVariable will throw error.