NODE_ENV is undefined in lambda functions no matter what I do

I am setting up Sentry messages and need to access the NODE_ENV and the COMMIT_REF env vars in functions. However, all of them, including the CONTEXT are undefined both at build time and runtime. Although env vars that I set in the UI are available as expected.

const webpack = require(‘webpack’);
module.exports = {
mode: process.env.NODE_ENV === ‘production’ ? ‘production’ : ‘development’,
plugins: [
new webpack.DefinePlugin({
‘global.GENTLY’: false,
‘process.env.COMMIT_REF’: JSON.stringify(process.env.COMMIT_REF),
‘process.env.CONTEXT’: process.env.CONTEXT,
}),
],
};

I tried with a custom webpack.config.js:

const webpack = require('webpack');

module.exports = {
  mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
  plugins: [
    new webpack.DefinePlugin({
      'global.GENTLY': false,
      'process.env.COMMIT_REF': JSON.stringify(process.env.COMMIT_REF),
      'process.env.CONTEXT': process.env.CONTEXT,
    }),
  ],
};

and in the package.json
"build:lambda": "netlify-lambda build --config ./webpack.config.js ./lambda",

but this did not help.

I also tried to set env files in the netlify.toml and a bunch of other ways that did not work. It’s ridiculous that such a simple and much needed feature does not work out of the box and there is no documentation on how to make it work.

Any help appreciated!

Hey @sandorTuranszky :wave:t2:

I think I’ve got an answer for you :slight_smile: Ultimately I believe the root cause of what you’re facing is the strong split between environments - the Netlify Site Build environment, and the AWS Lambda Functions runtime environment. Not much gets shared between them by default.

So to walk through a flow a bit here, I’d say read this whole page to get a feel for what vars are available and such:

Then check out this build plugin that I think will help you out greatly

And in general, find more information about Build Plugins (which are super new! yay!) here:

That ought to do it!


Jon

Jon has as usual done an incredible job of providing a very similar answer to the one Netlify’s staff would have. The “missing piece” is one we’re working on writing up right now - TL;DR we only automatically “forward” environment variables from our UI, NOT from netlify.toml (nor automatically set like “$COMMIT_REF”), to functions. The plugin Jon linked is a good way to get there, or you can manually interpolate the variables as explained in this article:

1 Like

Thanks for the info. I understand that env variables are not available at runtime, but the thing is that they are not available at build time too. I found articles where webpack was used to further forward env vars but since they were undefined at built time, there was nothing to be forwarded. Here is the list of env vars which are undefined at build time and runtime for me. I also set context env in the netlify.toml file and none of them was available. This is confusing as I rely on documentation but it doesn’t work for me :frowning:

I will try all methods mentioned in this tread. Btw, I tried netlify-plugin-inline-functions-env which did not help. Maybe it’s because I installed it from the UI rather than in the netlify.toml file?

Hey @sandorTuranszky!

Happy to dig into this more but I think I may need you to share some code at this point because it’ll be tricky to get super-specific without seeing the actual nuts and bolts. This is all going to be about your build process - specifically the site-build process before the Functions get packaged and compressed down into their own files and sent off to AWS.

Could you share some of your Netlify.toml and Package.json files? I think that’ll give a good idea of what’s running when for starters :thinking:


Jon

1 Like

Hmm. They definitely ARE available at build time - I think you may need to reread the article I linked that shows you how to USE them at build time :slight_smile: But TL;DR having a process.env.X in your DEPLOYED javascript isn’t using them at build time. Having it run during build, and interpolating the value, is using them as I recommend.

If that doesn’t work, I’ll echo Jon that we could use to see how your code uses them and learn a bit about your deployment process to understand where the disconnect is.