This module helps you to control your environment variables in one place.
Run npm install --save environment-variable to install the package.
Instructions below show you how to use this module in your project.
create an env.json file in your root folder(the folder that includes your package.json file) or you can pass an object as a first argument to the .init() method of the module
include the module in your main js file.
call the .init() method of the module to initialize your variables.
and everything is done. Now you can access your variables
Here is an example of creating an env.json file in your root folder
Imaginary Root folder
node_modules/
env.json
index.js // main js file
env.json file [ env.json file should be in your root folder then this package can find it automatically. ]
const env = require('environment-variable');
env.init();
console.log(env.get('PORT'));
// logs 3000 to your console.
console.log(env.get("DB").DB_USER);
// logs SYSDBA to your console.
Here is an example of passing an object as a first argument to the .init() method.
main js file [here, it is an index.js file]
const env = require('environment-variable');
env.init({
PORT: 3000,
USER: "simpleUser",
DB: {
DB_USER: "SYSDBA",
DB_PASSWORD: "masterkey"
},
arr: [1, 2, 'a', 'b', {name: "simpleName", newArr: [1, 2, 3, {nested: ["first", "second"]}]}]
});
console.log(env.get('PORT'));
// logs 3000 to your console.
console.log(env.get("DB").DB_USER);
// logs SYSDBA to your console.
console.log(env.get("arr")[4].name);
// logs simpleName to your console.
Here is an example of using an optional json file. [creating a json file that's name is different than env.json. e.g. var.json]
Imaginary Root folder
node_modules/
config/
optional.json //this is an optional json file that you can use in your app insted of env.json file
index.js // main js file
optional.js file [you will use this file insted of env.json file.]
const env = require('environment-variable');
const optionalFile = require('./config/optional.json');
env.init(optionalFile); // you have to pass the your optional.json file. If you don't pass an argument, env.json file will be used automatically.
console.log(env.get('PORT'));
// logs 5000 to your console.
console.log(env.get("DB").DB_USER);
// logs SYSDBA to your console.
Here is a list of all methods of this module.
Methods
Descriptions
init()
Initializes your variables. Calling this method is important. Call this method before accessing your variables.