I am trying to access my environment variables from aws parameter store, but I am not sure how can I access them in all the files without making it global or storing them in process.env. Is there any safer approach for that. I wanted to use exports those variables but since export processes at the run time and my aws parameters comes after that. Thanks.
Note:- I am not using serverless environment where I can directly access those by variable name.
Environment variables is a really bad way to communicate within a single application. They are usually used as one solution for communicating between several apps.
The normal way to store and fetch configuration parameters in your app is to use a module. Yes, modules typically contain code but there is nothing stopping you from creating a module that stores only data. Indeed, this is a fairly standard mechanism to store settings.
JSON file
For fixed configuration values such as a config file the standard is to just use a json file. For example:
config.json:
Then you can just require the json file wherever you need it:
main.js:
some_module.js:
JS file
Some people feel that JSON is too restrictive because you cannot put comments in a JSON file. Most people normally export an object form a regular js module:
lib/config.js:
main.js:
some_module.js:
One thing to understand about how
require
works is that it will cache the value ofmodule.exports
generated by a module. This makes node modules behave like singletons. In the example above, theconfig
variable in bothmain.js
andsome_module.js
point to the same object! I'll repeat that again because it's important: in the example above there is only one config object created by node.js. What this means is that you can use a pure-data module like theconfig.js
file to communicate between modules in your code. For example, you can do this:main.js:
some_module.js:
JS file that reads config from somewhere else
Because we are using a normal module to communicate configuration data with the rest of our code we can run any algorithm we want to generate configs:
config.js:
As the example above shows. You can use any logic to construct configuration data. You will need to be a bit more creative if any config data comes from an asynchronous source such as a database but you can just wrap the whole module in a promise and just await the config.
In general configuration should be kept in files. Not environment variables. Environment variables should store things about the environment for example where is the path of your C compiler or where is your web browser or what language is this computer configured to use as default. Things like process port numbers should not be stored in environment variables though it is traditionally (by traditional I mean the Unix tradition developed from the 1960s to today) acceptable to use environment variables to override configs to aid debugging.
Depending on the OS you are on there are various places that people traditionally store config files: the
/etc
directory on Unix,Application Data
on Windows,~/.config
directory on modern unix desktops etc.There are several npm modules that can help you organize your config files. Depending on your need you may find rc or config or configstore useful. Personally I'd like to promote my own config-default-cjson module but the other modules above have more features.