3

First time using Astro so I'm still figuring out my way around

I can call import.meta.env.SOME_VARIABLE in astro components with no issue, but calling import.meta.env.SOME_VARIABLE, even just in a console.log(), in any page results in an error.

Unterminated string literal 

or

Unterminated regular expression

Depending on what line number the env file is called on, one of the above errors will occur, leading me to think it's inserting some text or code into the existing code?

Again, no issue with calling env within an astro component, but the error occurs on all astro pages. Both pages and components are rendered server-side.

The root directory looks like this:

src
ー pages
ー components
astro.config.mjs
.env.local
.env.development
.env.production

Tried:

  • set to be a client-side public variable (PUBLIC_SOME_VARIABLE), but same error.
  • called process.env.PUBLIC_SOME_VARIABLE instead of import.meta.env, but it returned undefined as expected.

While I've found a workaround for now using the env variable in an astro component, I expected to be able to reference env variables in pages using import.meta.env

https://docs.astro.build/en/guides/environment-variables/

1
  • This is something working in other projects, it is here necessary to troubleshoot, probably sharing a reproduction example is necessary or add more info, Astro version and all project files e.g. astro.config.mjs,... Commented Dec 15, 2023 at 8:02

1 Answer 1

2

I had the same issue and spend a lot of time on it, until I realized I had const myVar = import.meta.env.MY_VAR above imports. Moving the variable initialization below imports solved my problem

Sign up to request clarification or add additional context in comments.

1 Comment

I had exactly the same problem, and this solved my issue. However, I am still trying to figure out why.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.