Skip to content

How do I use ENV variables in static .js files? #17696

Answered by bluwy
JorensM asked this question in Q&A
Discussion options

You must be logged in to vote

import.meta.env.* can only work for code that Vite processes, so if you want to use it in static files (which aren't processed by Vite), you need to manually change it yourself.

I think the easiest way is to write a script or Vite plugin that runs after the Vite build that replaces the import.meta.env.* in /dist/static-script.js.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@JorensM
Comment options

@aleborile
Comment options

Answer selected by bluwy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
3 participants
Converted from issue

This discussion was converted from issue #17695 on July 16, 2024 09:23.