Probably a dumb question, but given that serverless functions can access my env variables defined for my site when running locally with netlify dev
, would other scripts also have access? I’m using Eleventy and running it locally with netlify dev. I’m trying to access the env variables in one of Eleventy’s JavaScript files, not a serverless function. That should work, right?
Hey! No dumb questions here
So, as stated here, environment variables that are available during the build process [such as those in the UI] won’t be available to your client-side UI unless you interpolate them at build time. Some frameworks do this for you automatically. For 11ty, you’ll need to expose them.
We have a great write-up on how to use environment variables correctly which may also be useful!
TL:DR; it’s doable !
This isn’t client-side UI though. It’s an Eleventy data file run when the generator is - well - generating. When I use netlify dev with serverless functions, I see the CLI report that it’s loading env variables I set on the Netlify side. I’m not seeing that now.
Hmm, not sure what the behavior would be for locally set variables; certainly some build pipelines remove them before starting (though I do not think that is the case with yours if you can see them).
How do you “run” eleventy’s JS files? Obviously our web service won’t be running at browse time, so you’d have to interpolate during build, but I think you know that…so maybe I could use some more details as to how you’re setting, and where you are and aren’t seeing the values, and anything else that might be relevant?
They are run as part of 11ty’s build process. So for example, a file in _data called foo.js will execute and can provide data to site templates.
To be clear, my usage of that works just fine in production. Netlify’s env vars I set for my site are visible just fine. I’m ONLY talking about use with “netlify dev”.
In the past when using serverless functions, I’ve had no issue with env vars working.
Just as an update, I thought perhaps the issue was that netlify dev would only “suck down” environment variables if it detected a serverless function. However, I’ve added a serverless function to my project and netlify dev still refuses to “download” the environment variables I’ve setup in my site.
Hmm. it shouldn’t need a lambda function for the env vars to work. It sounds like a bug. Would you mind filing one here: Issues · netlify/cli · GitHub since you would probably be able to add more context?
Done!