Environment variables can't be used in config files#1567
Conversation
✅ Deploy Preview for astro-docs-2 ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
delucis
left a comment
There was a problem hiding this comment.
You can access environment variables using process.env in the config file, with the caveat that .env files are NOT loaded yet. An environment variable set by a CI service or on the CLI will be accessible though. For example, you could use process.env.NETLIFY to toggle a feature only in Netlify builds or use TOKEN=secrets npm run dev to expose process.env.TOKEN.
I think I've also heard people are using dotenv to load their .env files, so that could be a workaround if it doesn't have drawbacks — Nate would know best!
|
@delucis Would |
|
No, |
|
Added a section on setting variables with the CLI. I moved the caution in Environment Variables to the overview. It really is an overview, not an introduction, and I wasn't sure whether to document
|
delucis
left a comment
There was a problem hiding this comment.
I think these are good additions, Dan — thank you!
It’s tricky deciding where to document process.env but I think I agree with where you ended up — the most common use case is in the astro config file so makes sense to document it there.
I could leave a bunch more comments on the rest of the environment variables page but I’ll hold off until you say that would be helpful.
|
More comments as requested by @Jutanium (to be added to this PR or tackled later at his discretion).
|
|
Thanks @delucis! This page could use a broader reworking (including explaining what Environment Variables actually are), but I made minimal changes for now to address your points. I removed the .env files snippet entirely as it was copy-pasted from Vite docs - I linked to that instead. I also removed the the tip rather than moving it, as we made this point clear as soon as we talked about client-side code in the intro. |
* note that env variables can't be used in config files * add note to configuring astro * rephrase and mention process.env * add section on setting env variables with cli * move the caution to the overview * fix links * reformat default environment variables section * replace .env file snippet with reference to vite docs (where it was copied from) * remove unhelpful tip * add back consistent casing
What kind of changes does this PR include?
Description
We closed #785 as the corresponding feature was closed, but the note about Astro being unable to load env variables is still important information, even if we don't have a workaround. This PR restores that note and adds a corresponding one to the environmental variables change.
I could see us only wanting one of those two messages, or rather wanting this to be in Troubleshooting. Let me know!
@natemoo-re, if there's currently a workaround to access env variables in Astro, let me know.