-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handling server-side/sensitive/runtime variables #3176
Comments
It seems to me that, at best, what the current If removing this replacement isn't a viable option for compatibility reasons, then I think this should be configurable in some way - if not in all cases, then at least in SSR situations, where there is a legitimate reason to be using |
Why not replace it with optional chaining and (globalThis.process?.env ?? {}). And ofc, making this configurable is another option |
Been futzing around with The current lack of solutions is highly frustrating. |
Due to vitejs/vite#3176, we have to use `process.env['FOO']` rather than `process.env.FOO`.
Due to vitejs/vite#3176, we have to use `process.env['FOO']` rather than `process.env.FOO`.
Until there's a solution for this, is safe to import |
* fix: workaround vite replacing process.env in updater vitejs/vite#3176
Context
Currently, Vite indiscriminately replaces
process.env.
with({}).
:vite/packages/vite/src/node/plugins/define.ts
Lines 33 to 38 in 7cd8d78
I'm not sure if this behavior is Vite is attempting to protect SPA authors from accidentally usingConfirmed with Evan that this was to support libraries that indiscriminately useprocess.env.SECRET
in their code.process.env.NODE_ENV
.The Problem
Applications that have a server-side encounter a common issue of needing to be able to access sensitive information from environment variables, e.g. database or API credentials. Aside from secrets, it is also possible to require runtime variables that you don't know at build time. Currently people are working around this using
process.env['SECRET']
to avoid the string replacement, but it is entirely possible that in the future Vite decides to similarly replaceprocess.env[
with({})[
given the above code. I am unsure if this is the "blessed" way to access secrets or an oversight, considering it completely circumvents the concerns written above.Using
VITE_
prefixed environment variables is not a solution. It is explicitly documented that it should not contain sensitive information.Using define is not a solution:
(e.g. heroku database URLs can change across application reboots, so runtime ENV vars are required)
Potential Solutions
(in no particular order)
process.env['SECRET']
as a "blessed" way to access runtime variables, although then it is unclear whyprocess.env.
is replaced in the first place.process.env.
- This replacement behavior is not currently written in the docs.process.env.
replacement.Non-solutions
process.env[
with({})[
if you're sadisticThe text was updated successfully, but these errors were encountered: