Netlify Edge Function w/ Build Environment Vars

Hi,

I’ve been trying to deploy an edge function. I follow the hello_world example from the blog and it works. However, when I try to read environment variable from Netlify, it failed.

https://xindian.netlify.app/test

This is the code learn/hello.js at master · Bojne/learn · GitHub and I attempt to follow this Deno doc on reading env var. https://deno.land/x/dotenv

I’d like to know if it is possible to read env var at edge function?

Deploy log id: 62602b5ff152f30008c12b7f
Deploy log:

5:49:16 PM: Error: file system access error (code 42), id:6fa8a341-0709-4982-ac16-c098093dbe78
    at async Object.readFile (deno:deploy/js/02_fs.js:53:12)
    at async parseFileAsync (netlify:bundle-combined:940:54)
    at async configAsync (netlify:bundle-combined:887:18)
    at async netlify:bundle-combined:999:1
5:49:16 PM: Error: file system access error (code 42), id:6e85cc79-b622-4b4c-9115-f87bb3f68c84
    at async Object.readFile (deno:deploy/js/02_fs.js:53:12)
    at async parseFileAsync (netlify:bundle-combined:940:54)
    at async configAsync (netlify:bundle-combined:887:18)
    at async netlify:bundle-combined:999:1
5:53:38 PM: Error: file system access error (code 42), id:f5bcaf2b-8b21-4bb4-a90c-fad6b6c7bd67
    at async Object.readFile (deno:deploy/js/02_fs.js:53:12)
    at async parseFileAsync (netlify:bundle-combined:940:54)
    at async configAsync (netlify:bundle-combined:887:18)
    at async netlify:bundle-combined:999:1
5:53:38 PM: Error: file system access error (code 42), id:8ce28dae-5880-4510-a3fc-14fd64efb281
    at async Object.readFile (deno:deploy/js/02_fs.js:53:12)
    at async parseFileAsync (netlify:bundle-combined:940:54)
    at async configAsync (netlify:bundle-combined:887:18)
    at async netlify:bundle-combined:999:1

Hey there, @Bojne :wave:

Thanks so much for reaching out about this. We will investigate this for you and follow up when we have more information. In the interim, please let us know if anything changes.

Sure! I’d almost guessing that it Edge function is quite “independent” so loading env variable will not work. But would love to know if the Netlify team have recommendations on using environment variable on the edge!

Hey @Bojne

While I haven’t tried using environment variables myself yet, there is mention of them in the Edge Functions API documentation

Deno.env : Interact with environment variables

1 Like

I was able to read env vars in an edge functions via Deno.env

export default () => new Response(
  JSON.stringify(Deno.env.toObject(), null, 2));

Code: netlify-edge-functions-test/hello.ts at main · anishkny/netlify-edge-functions-test · GitHub

URL: https://netlify-edge-functions-test.netlify.app/hello

1 Like

Thanks for coming back and letting us know! Happy building :rocket:

Just hit this same problem, but with a slightly different solution, so documenting here for others:

We also hit issues that Deno.env didn’t work locally in VS Code, just returned all the API keys as undefined, in order to get Deno working locally we also needed to run:

import { configAsync } from 'https://deno.land/x/dotenv/mod.ts';
await configAsync({export: true});

The challenge here is that this works locally, but breaks when deployed to Netlify. As such, we can wrap the configAsync in a try/catch:

try {
  // when running locally, we need to call this
  // it'll eerror in Netlify prod, but that's okay
  await configAsync({export: true});
} catch (err) {
  // ignore the error on netlify Prod
}

Hi @joepavitt thanks so much for sharing your solution with the community. This is insightful and very helpful to our members.

1 Like