ENV Loader Documentation
A node.js package that lets you load your env variable from your specified list of files.
On this page
About
It is an npm package that helps developers load their environment variables from a given list of files. It can also load all the variables into a custom variable provided or add them to Node's global variables, among other capabilities.
Package Requirements
This package does not require any additional software, package or library except Node.js to run.
Installation
Follow the following commands
- On your computer type shortcut
CTRL+ALT+T
for ubuntu andWin+X
then I for windows to open your terminal. - Go into your project's root directory.
- Now copy the following command to install the package in your project.
npm install @book-junction/env-loader
Usage
The package is easy to use. Follow the following code in order to use it.
const EnvironmentVariables = require('@book-junction/env-loader');
Kindly note
After importing the package, EnvironmentVariables
variable has now access to all static functions provided by the package. Each function servers a different use case. All use cases are discussed in upcoming sections. Now, we will first start with our 1st function i.e., LoadENV
LoadENV function
Function Definition
static LoadENV(envPath='[.env]');
Function Insights
- Returns -
void
- Parameters
- envPath represents an array of file paths to search for environment variables. By default the list is
[.env]
.
- envPath represents an array of file paths to search for environment variables. By default the list is
- Function type - static
- Function Time Complexity - O(n)
- Function Space Complexity - O(1)
Function Usage
EnvironmentVariables.LoadENV(['.env','.env.development']);
Pay attention
If the package does not find any of the specified files in the project's root directory, it will throw an error. Please handle this gracefully.
Function Description
This function loads all the environment variables from the first found file (from the given list of file paths). For example, if a second file is present in the project root directory, it will load all the environment variables from that file.
LoadENVIntoVariable function
Function Definition
static LoadENVIntoVariable(['.env','.env.development']);
Function Insights
- Returns -
void
- Parameters
- envPath represents an array of file paths to search for environment variables. By default the list is
[.env]
.
- envPath represents an array of file paths to search for environment variables. By default the list is
- Function type - static
- Function Time Complexity - O(n)
- Function Space Complexity - O(1)
Function Usage
EnvironmentVariables.LoadENVIntoVariable(['.env','.env.development']);
Pay attention
If the package does not find any of the specified files in the project's root directory, it will throw an error. Please handle this gracefully.
Function Description
This function loads all the environment variables from the first found file and store it in class variable called ENV. For example, if a second file is present in the project root directory, it will load all the environment variables from that file into class variable ENV.
LoadENVIntoCustomVariable function
Function Definition
static LoadENVIntoCustomVariable(['.env','.env.development'],customVariable);
Function Insights
- Returns -
customVariable
- Parameters
- envPath represents an array of file paths to search for environment variables. By default the list is
[.env]
. - customVariable is a variable in which the all environment variables will be loaded. By default the variable is
{}
.
- envPath represents an array of file paths to search for environment variables. By default the list is
- Function type - static
- Function Time Complexity - O(n)
- Function Space Complexity - O(1)
Function Usage
const ENV = {}; // Variable in which env variable will be loaded.
EnvironmentVariables.LoadENVIntoCustomVariable(['.env','.env.development'], ENV);
Pay attention
If the package does not find any of the specified files in the project's root directory, it will throw an error. Please handle this gracefully.
Function Description
This function loads all the environment variables from the first found file and store it into custom variable provided as a 2nd argument. For example, if a second file is present in the project root directory, it will load all the environment variables from that file into provided variable.
LoadENVIntoGlobals function
Function Definition
static LoadENVIntoGlobals(['.env','.env.development']);
Function Insights
- Returns -
void
- Parameters
- envPath represents an array of file paths to search for environment variables. By default the list is
[.env]
.
- envPath represents an array of file paths to search for environment variables. By default the list is
- Function type - static
- Function Time Complexity - O(n)
- Function Space Complexity - O(1)
Function Usage
EnvironmentVariables.LoadENVIntoGlobals(['.env','.env.development']);
Pay attention
If the package does not find any of the specified files in the project's root directory, it will throw an error. Please handle this gracefully.
Function Description
This function loads all the environment variables from the first found file and store it into node's global variable called $ENV. For example, if a second file is present in the project root directory, it will load all the environment variables from that file and load into $ENV variable.
Where it can be used?
This package is useful in every project where developers work with environment variables and need to load them from different files based on the project environment.
Availability
This package is hoised live on npmjs official web site. You can download it from there as well.