Environment variables in NodeJS depenency - node.js

Environment variables in NodeJS depenency
I have a NodeJS application which has a dependency of mine
my-base-module: git+https://myuser:mytoken#gitlab.com/organization/my-base-module.git#v1.0.0
I am also using the dependency dotenv, which in development I use the file .env and in other environments I pass the variables through docker environment variables
The problem I have, and I do not know why and how to solve it is that in my-base-module I do not share the same environment variables (and I need it to). It is like the variables loose the reference
require('dotenv').config();
const env = process.env.NODE_ENV;
for example in that bit of code the NODE_ENV variable inside my-base-module is undefined. However in the container is defined and with the right value
Update 1
I am requiring dotenv in both my-application and my-base-module. If I enter to the container and I do $ echo $NODE_ENV. I get production.
If my application process.env.NODE_ENV also holds production. But in the dependency process.env.NODE_ENV is undefined
I will try to do some github repositories to reproduce it

I was wrong, this works I made this small POC with these public repositories
https://github.com/agusgambina/try-base-module..https://github.com/agusgambina/try-application-dotenv
https://github.com/agusgambina/try-application-dotenv

Related

.env variables not available in jest testing environment

When I run my code normally I can access my .env file with const newVar = process.env.MY_DOTENV_VARIABLE, but when I run jest everything becomes undefined. Is this normal for jest? If so, what is the best practice for storing variables?
Is it simply to create a set up file, eg:
// jest.config.ts
setupFiles: [
"<rootDir>/.jest/setEnvVars.ts",
],
# .env
MY_DOTENV_VARIABLE=exampleString
I just needed to install dotenv. I think I got confused where process.env was working previously without the dotenv packaging. This was either due to me setting the env variables with scripts in my package.json file, eg "scripts":"NODE_ENV=test ..." and/or some packages were making changes. (I'm using various aws packages, and I've read that they can change environment variables)

Using environment variables in Node

I have been trying to get a streamline way of having different environment variables for local and production web apps, but I haven't come across the "ideal" solution yet.
There's the option of having a config.js file like so:
//config.js
{
"secretKey": "SDFDASFFSFD",
"facebook": {
"clientID": "EFGFDGBGDGFS",
"clientSecret": "EGDFNHFG"
}
}
And accessing via ES6 imports
Or using .env files like so:
SOME_KEY=someValue
HELLO=world
FACEBOOK_SECRET=435SDFSF5DZVD7S
And accessing the variables via process.env in the code using dotenv.
Obviously no matter what route you go down, the file will need to be omitted from version control which is fine. Each of these ways are great, but they only seem to work well for local development.
So how do you then have a separate file for a production environment? The dotenv docs say they strongly recommend against a .local.env and .prod.env situation.
Also, how is best to push to a remote server? I have my own server with Gulp tasks which run on a Git post-receive hook. How is best to pass up the production environment variables to here?
Thanks
You could have own config file for each environment:
- environments
- index.js
- deveplopment.json
- staging.json
- production.json
To use appropriate config file, run the app with required NODE_ENV:
NODE_ENV=production node index
In environments/index.js determinate the current NODE_ENV and use config:
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
module.exports = require('./' + process.env.NODE_ENV);
If config file doesn't include secret info (apiKeys, etc), it can be pushed to repo. Otherwise add it to .gitignore and use environment variables on the server.
Note:
For advanced configuration use such packages as nconf.
It allows to create hierarchical node.js configuration with files, environment variables, command-line arguments, and atomic object merging.
Have you thought about having a keys.js file with a .gitignore on it?
I've used this in the past to use module.exports{} to get my variables usable but not going to version control for the security side of things!

AWS Lambda Environment Variables not set, process.env undefined in Node

Even though the dashboard for the Lambda function shows me NODE_ENV listed as an environment variable, process.env.NODE_ENV is undefined when I run my Node AWS Lambda function.
This code...
console.log('node process env NODE_ENV:');
console.log(process.env.NODE_ENV);
logs this:
node process env NODE_ENV:
undefined
I was wondering if it was a role issue, so I gave its role all permissions on AWS Lambda, but it didn't help.
For what it's worth, I'm using Claudia bot builder, running the npm script
"deploy:production": "claudia update --version production --set-env NODE_ENV=production",
But like I say, the NODE_ENV environment shows in the list of environment variables in the dashboard for the function.
Any ideas?
Turns out the problem was that Webpack compressed away process.env.
I got around the issue by using babel-plugin-transform-inline-environment-variables, which inlines the environment variables at build-time.
If you got here from a google search:
This could also happen when you define a function named "process" in your code. The function will override the internal node.js "process" variable.
I found out as long as you put target: node in your webpack configuration, you will get access to the process.env variables.

how to configure express js application according to environment? like development, staging and production?

I am new to expressjs app development, now need to configure application as per environment. came across 'node-env-file' , 'cross-env'. but hardly understood anything. Pls suggest how to set env variables as per environment or some good documentation suggestions pls?
Based on environment, I would like to load my configuraiton file.As of now, I have two config files, one for dev and one for production.
The idea is to set NODE_ENV as the environmental variable to determine whether the given environment is production or staging or development. The code base will run based on this set variable.
The variable needs to set in .bash_profile
$ echo export NODE_ENV=production >> ~/.bash_profile
$ source ~/.bash_profile
For more, check Running Express.js in Production Mode
I follow Ghost.org (a Node.js production) app's model.
Setting environments
Once that is done, you can have the environmental details in respective json files like config.production.json, config.development.json
Next, you need to load one of these file based on the environment.
var env = process.env.NODE_ENV || 'development';
var Nconf = require('nconf'),
nconf = new Nconf.Provider(),
nconf.file('ghost3', __dirname + '/env/config.' + env + '.json');
For more on how Ghost does this, check config/index.js
I use a .env file with env vars:
VAR=VALUE
And read that with source command before running express
$ source .env
$ node app.js
Then, to access inside express, you can use:
var temp = process.env.VAR; //contains VALUE
You can use dotenv module, to automatically load .env file

Node JS, detect production environment

When I'm in dev mode I don't want my server to send email and some other stuff so I have this in server.js file:
process.env.NODE_ENV = 'development';
When I move it to production i change the value to 'production'.
Problem is of course that it is easy to forget on deployments. Is it possible somehow to detect when server is in production?
If it's not, is it possible to write a batch script that replaces a string in a file?
You shouldn't manually change values of process.env object, because this object is reflecting your execution environment.
In your code you should do the following:
const environment = process.env.NODE_ENV || 'development';
And then you launch your app in production like this:
$ NODE_ENV=production node app.js
If NODE_ENV environment variable is not set, then it will be set to development by default.
You could also use dotenv module in development to specify all environment variables in a file.
Furthermore, I've implemented a reusable module, which allows to automatically detect environment by analyzing both CLI arguments and NODE_ENV variable. This could be useful on your development machine, because you can easily change environment by passing a CLI argument to you Node.js program like this: $ node app.js --prod. It's also nice to use with Gulp: $ gulp build --prod.
Please see more details and use cases on the detect-environment's page.
The suggested way to tackle these kind of problems is by using the process global object provided by NodeJS. For example:
var env = process.env.NODE_ENV || "development";
By the above line. We can easily switch between development / production environment. If we haven't configured any environment variable it works with development environment.
process.env is an object that contains the user environment.
For example if we are running our application in a bash shell and have configured NODE_ENV to production. Then i can access that in node application as process.env.NODE_ENV.
There are multiple ways to define this NODE_ENV variable for nodejs applications:
## Setting environment variable for bash shell
export NODE_ENV=production
## Defined while starting the application
NODE_ENV=production node app.js
So i don't think you would require any batch file for this. When handling multiple configuration variables, follow this.
This is normally handled through configuration files (e.g. config frameworks like node-convict) and through environmental variables. In the start command in production, you might do something like:
NODE_ENV=production npm start
then the process.env.NODE_ENV would be properly set by any module in your app that cares.

Resources