Netlify global in functions v2

The documentation for functions since v2 says that there is a Netlify global object, and that this object has an env field for getting environment variables. However, when my function attempts to use the Netlify.env interface I get a reference error:

"ReferenceError: Netlify is not defined"

indicating that the Netlify object is probably absent. Is the documentation incorrect?

This is supposed to be fixed: fix: make Netlify.env working by injecting it in the entry file by lukasholzer · Pull Request #1618 · netlify/zip-it-and-ship-it (github.com), which has since been added in this version: Release functions-utils: v5.2.37 · netlify/build (github.com).

I think it would be released in the coming week. Let us know if not.

Any progress on this? I’m still seeing the issue.

Are you sure you’re using v2 API? It won’t be available in the Lambda API.

I’m certain. I re-engineered existing functions to export default, receive Request objects and return Response objects. If I was on old functions they’d throw an error very quickly (upon using the headers interface). They otherwise work exactly as v2 functions are documented. There’s simply no Netlify global object. I am, however, able to use the Node.js process.env global.

Edit: To be clear, these functions are in production and do the job they’re supposed to do. I rely on global access to environment variables though, so I’m using a wrapper function which attempts to get environment variables from Netlify.env(...), or falls back to process.env[...] (logging whichever worked). The logs from this are how I know there’s still a problem with Netlify.env. You can see that wrapper here (not my finest code, be kind).

Interesting. I have asked the devs to take another look.

Hey @qubyte, thanks for reporting this! I looked into it, and was able to reproduce it with this function:

const bar = Netlify.env.get("FOO")

export default () => {
  return new Response(bar);
};

export const config = { path: "/*" };

interestingly, this function works:

- const bar = Netlify.env.get("FOO")

export default () => {
-  return new Response(bar);
+  return new Response(Netlify.env.get("FOO"));
};

export const config = { path: "/*" };

So the problem seems to be that user code is being evaluated, before our system code populates the Netlify global. I’ll look into how we can fix that!