'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?



Solution 1:[1]

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.

Solution 2:[2]

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.

Solution 3:[3]

I found out as long as you put target: node in your webpack configuration, you will get access to the process.env variables.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Y.L
Solution 3 aminh101sj