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?
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 forgot to report back, but we fixed the original issue a couple days after my last post. Are you seeing this with the Netify CLI locally, or when Netlify builds it for you? If it’s the CLI, what version are you on and does it still exist with the latest CLI version? Could you share the function code you’re seeing this error on?
Feb 11, 07:31:02 PM: fc36f5d7 ERROR Invoke Error {
“errorType”:“ReferenceError”,
“errorMessage”:“Netlify is not defined”,
“stack”:[
“ReferenceError: Netlify is not defined”,
" at Runtime.handler (/var/task/netlify/functions/…
at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)
"
]
}
And on screen:
This function has crashed
An unhandled error in the function code triggered the following message:
ReferenceError: Netlify is not defined
at Runtime.handler (/var/task/netlify/functions/...
at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)
Colin, could you share the Function code for this request? That should help me reproduce.
Also, the Netlify global only exists in Functions 2.0, and the stacktrace you mention makes me think you’re using Lambda Compatibility mode aka “old functions”. Could that be the case?
Aha! Yes, that’s a “Lambda Compatibility Function”, aka the old way of authoring Netlify Functions. This doesn’t have access to Netlify.env in production. There’s some technical javascripty reasons for why we can’t make local dev behave exactly the same, so you’ll see Neltify.env be available locally, while it doesn’t in prod.
There’s two things you can do to work around this:
Use process.env.VITE_OPENAI_API_KEY instead of Netlify.env.get("VITE_OPENAI_API_KEY")
OR
Migrate to using a Netlify Function 2.0. Here’s how that would look:
Personally, I think Functions 2.0 are much more concise in how they’re written, and just a little more fun. If I were you, I’d opt to rewrite the Function - but you do you!