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

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.

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)

Environment variables in NodeJS depenency

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

Difference in environment variable usage between development and production modes?

I'm learning how to deploy a simple backend node.js project to production, in my case heroku. I came across this note regarding environment variables in a .env file:
The environment variables defined in dotenv will only be used when the backend is not in production mode, i.e. Heroku.
We defined the environment variables for development in file .env, but the environment variable that defines the database URL in production should be set to Heroku with the heroku config:set command:
heroku config:set MONGODB_URI=mongodb+srv:...
How can my .env file use the heroku config:set MONGODB_URI=mongodb+srv... environment variable, if using heroku would mean that my backend is in production mode? The first sentence states that environment variables are only used for development mode.
What am I understanding wrong? Are environment variables used in both development mode and production mode, and the wording of the note I read was wrong?
I think it's saying that environment variables in .env will be used when you're in development (not Heroku) and that if you do want to use your environment variables in Heroku that you need to do it through heroku config:set ....
What am I understanding wrong? Are environment variables used in both development mode and production mode, and the wording of the note I read was wrong?
Environment variables are used in all scenarios, but only in local development mode they are read from a file.
dotenv is a package that allows reading environment variables from a file.
In production mode, what Heroku is essentially doing is this, for example:
PORT=9999 node index.js
Try it, you can access this variable inside Node by using:
console.log(process.env.PORT);
// 9999
Environment variables are just global variables available to the entire process.

How to get environment variable without process.env

How I can get environment variable without process.env, because it is undefined, due to launched app by npm run serve command.
p.s. My app is single Vue page.
place the .env file in your project dir,
inside this file you can write key value variables. Like
SECRET=mySecret
for mor info: vue cli dock

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