Skip to content

Environment variables can't be used in config files#1567

Merged
Jutanium merged 10 commits into
mainfrom
env-vars-config
Sep 19, 2022
Merged

Environment variables can't be used in config files#1567
Jutanium merged 10 commits into
mainfrom
env-vars-config

Conversation

@Jutanium
Copy link
Copy Markdown
Contributor

What kind of changes does this PR include?

  • New or updated content

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.

@netlify
Copy link
Copy Markdown

netlify Bot commented Sep 14, 2022

Deploy Preview for astro-docs-2 ready!

Name Link
🔨 Latest commit 0de36af
🔍 Latest deploy log https://app.netlify.com/sites/astro-docs-2/deploys/63288dff4684a500081e02cc
😎 Deploy Preview https://deploy-preview-1567--astro-docs-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

Copy link
Copy Markdown
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@Jutanium
Copy link
Copy Markdown
Contributor Author

@delucis Would import.meta.env.NETLIFY work in a config file? (I guess I could deploy to Netlify and see for myself!)

@delucis
Copy link
Copy Markdown
Member

delucis commented Sep 14, 2022

No, import.meta.env is a Vite thing and the config is evaluated in a standard Node environment before Vite is running.

@Jutanium
Copy link
Copy Markdown
Contributor Author

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 import.meta.env is already brought up by that point.

I wasn't sure whether to document process.env in Environment Variables or Configuring Astro. I chose the later because Configuring Astro seems to want to be comprehensive, but I could see other possibilities:

  • I could do the opposite where I just have a section in Environment Variables that has all the info, and don't change Configuring Astro.
  • Or I could have a caution in Configuring Astro that links to Environment Variables, where the info actually lies.

Copy link
Copy Markdown
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/pages/en/guides/environment-variables.md Outdated
@delucis
Copy link
Copy Markdown
Member

delucis commented Sep 19, 2022

More comments as requested by @Jutanium (to be added to this PR or tackled later at his discretion).

  1. Opening paragraph:

    Astro uses Vite for environment variables

    I’d probably rewrite to something like

    Astro relies on Vite’s built-in environment variable support

    Or even rewrite the whole opening sentence:

    Astro lets you use any of Vite’s methods to get and set environment variables.

  2. “Default environment variables” section

    Totally a nit but in the first list item I’d write either “development | production” or “development or production” but not use the | outside of <code>.

  3. “Supported file names” code snippet

    Could probably considered a nit, but might reformat as a table or list. At first glance I read it as a continuation of the .env file example above it.

  4. “Getting environment variables” section

    The browser support tip seems excessively noisy in this context. I’d roll that content into the opening paragraph.

@Jutanium
Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Dan! LGTM 🙌

nokazn pushed a commit to nokazn/docs.astro.build that referenced this pull request Oct 2, 2022
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants