Where to store node / express environment variables? - node.js

I am new to node/express and was wondering
where should I store environment variables?
where I should store config files?
if there's an example out there on how to configure the config file?
Thanks!!

You can store your config files under config dir and load them based on your requirement.
As for environment variables, if you mean the variables passed during run time execution; then I believe you can directly access them under your project. If you mean to store project level information then you can use something like project.json and also have env variables in it.
You can use this project as reference for both the tasks.

Related

Deploy Dockerized NodeJS application with the respective .env files

I am working on a NodeJS application which is a containerized application. I use Jenkins to build and deploy it. I have an environment .env file and along with this, also have .env files based on environments.
For example: .env.DEV, .env.SQA, .env.STG and .env.PROD.
Each file has different values for the variables based on environments.
When I am deploying my application, it always fetches variables from the .env file instead of specific environment file i.e. .env.DEV (if deploying on DEV server).
How do we use specific environment file while doing the deployment on Jenkins?
Note - I followed this great content on dotenv library but I didn't find anything helpful for my use-case. I even Googled a lot but didn't find much on this. Any help would be greatly appreciated.
You can use dotenv-flow which does exactly this, given the value of NODE_ENV environment value it will load the expected environment.
You also will need to make sure that the container receives the proper environment values from jenkins, this might help.

Is there a way to validate if all environment variables in the code exists in the .env file?

I'm trying to find a way to validate if all of the environment variables used in the code are actually set in the .env file
Something like a test to run in the pipeline that throw a warning if I'm missing a variable in the environment I'm currently deploying.
Context
Nodejs
It is a big application with a lot of environment variables.
There are multiple environments dev, qa, prod.
I'm using the package dotenv
dotenv.config()
process.env.A_VARIABLE
There is actually a package targeting exactly this issue: https://github.com/af/envalid
You can define a required schema for the .env file; if a condition is not met, it will end the process with a proper error message.
Furthermore, there are nice features like providing default values and loading a sanitized and typesafe env object for further usage in your application.

How to create a common environment specific property file in javascript

I am new to programming in node.js. I want to know if there is a way to create a environment specific property file, the same way we create in java and read from there. Is there a similar way we can do it in node.js? I have to connect to the db and it has user, connect string and password. I need that to be environment specific.
Most people use environment variables for environment-specific variables ;)
If you want to be able to supply those from a file, you can use the dotenv npm package to read defaults from a .env file, or you can use something like direnv if you don't want to alter your code, and just want to be able to specify environment variables automatically depending on your working directory.
I'd say that dotenv is the common way to do this for development environments.
Yes! You need .env files. See this guide for details: https://www.freecodecamp.org/news/nodejs-custom-env-files-in-your-apps-fa7b3e67abe1/

Environment Variables in App Engine for local dev

What's the best way to set environment variables when developing nodejs on a local machine for App Engine Flex environment? If they're set in app.yaml then they don't get set during local development. Is there a way to force this, or should I use something like dotenv and keep track of the same environment variables in 2 places?
Sensitive data (e.g. API Keys) should not be committed to source code.
The way I went around that is storing a .env file in Google Storage. Then you can use #google-cloud/storage to download it in production (using a prestart hook) and dotenv to load variables into memory.
You may find a full guide here: http://gunargessner.com/gcloud-env-vars/
PS: I would go for Aidan's answer for storing any data that is not sensitive. I have myself used dotenv satisfactorily in the past.
Similar to that, there's nconf, the package that gcloud itself uses for examples. Pretty neat!
Option 1:
require('dotenv').config({path: '/custom/project/root/app.yaml'})
Option 2:
Maintain both a .env file and a .yaml file with the same keys but different values (local and GAE, respectively). In app.yaml, I make sure not to deploy my .env file by adding the following line:
skip_files : .env
You will then need to add a check on ('dotenv').config() to make sure that it doesn't error or overwrite your process variables if no .env file is detected.
Aidan's suggestion is good.
As configurations should be different on GAE and local, I would suggest option 2 is best - a separate .ENV for local and .YAML for GAE environments.
One minor point though. I'd suggest to add a .gcloudignore files, something like the below:
.gcloudignore
.git
.gitignore
.env
staging.yaml
node_modules/

NodeJS config.json vs process.env for configuartion management

I've come across people using both methods to do config management.
What are the pros and cons of each approach?
If I have a lot of variables that I store in my config object, will I have to set them all one by one in an upstart script before executing the node app?
You generally use envvar to keep an application stateless. The same codebase should work in dev, staging, test and production environment.
You will put var like MySQL config, API keys, if log is enabled or not, if debug is on or not ...
Config file are used for variables which are not dependent of the environment. For instance, name of the application, number of items per page ...
I guess you can use config.json file for storing big configs. ENV I've usually use for passing application port or something very important for normal application start. For example if you use some external lib, it's better to be able to pass custom path to lib executor in ENV.
P.S. You should never save config.json in SVN.

Resources