Is using dotenv necessary if environment variables already declared with Docker - node.js

If I have declared a port variable or anything in the Dockerfile of a nodejs project as ENV PORT=8080. Is it still necessary or maybe best practice to use the dotenv package to load the environment variables as require('dotenv').config()?

Most of the Node.js applications relies heavily on environment variables.
It is a common thing to load these variables from a .env file with your mentioned library, however this is only a good practice and not a strict rule.

Related

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.

Does nodes give you a way to see all environment variables that it can use?

When I start to use an existing app/codebase, I'm often confounded by which environment variables it's wired to use. People make bad docs, and I hate hunting and being disoriented by this part of app dev.
Is there a way to see all environment variables that it can use? Like an npm run... task that lists them all statically?
Node.js can basically use all of your systems environment variables. try console.log(process.env) and you'll find that all environment variables that you've declared, even before starting your node app are showing.
You can also create new environment variables through node, by doing process.env.MY_ENVIRONMENT_VARIABLE = 'hello'. So i guess searching process.env through the whole project should list all env variables being accessed and created inside node.

Naming convention for environment variables files?

I was just wondering if there was any standardized convention for .env environment variable files. If I had multiple setups (e.g. development, staging, production), what should they be titled?
I've seen:
.env.development, ...
development.env, ...
settings.development.env, ...
.env, .env.staging, .env.production, ...
If there isn't a standard, are there arguments for which to use (kinda like "" vs '' for strings in JS)? Which do you use and why?
I prefer .env at the end ex: .production.env because last word after . indicates extension. VSCode does not recognize the file as .env file if you do .env.production
VSCode Documentation:
https://code.visualstudio.com/docs/python/environments#_environment-variables
There is only one standard in node ecosystem - use production in NODE_ENV variable for production. It's used by different tools for optimizing.
The rest is on you. But personally, I don't see reasons to make it complex. If you need to differentiate instances further than just production, development, you can use other environment variables.
Also you need to be careful when using different NODE_ENV for staging and production. Because the main purpose of staging is to test everything as close to production as possible. So changing NODE_ENV can be a reason to production fail.
I'm using this convention since I can't find any other suggestions: https://rubyonjets.com/docs/env-files/
.env
.env.development
.env.test
.env.production
I like maintaining the .env at the front and this seems reasonable to me.

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/

Where to store node / express environment variables?

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.

Resources