I like this method too, the downsides is that you need to add a package for production and import dotenv/config in every single file you use it.
But the good thing is that you can still use the VITE_ prefixed variables on the client, and non-prefixed ones in the server in files files hooks and endpoints, without the client know about them.
I only think VITE shoud expose non-prefixed ones to server only automatically, and VITE_ prefixed ones to both server and client, I simply don't know why on earth this isn't the default behaviour.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I first get it done by using your checking NODE_ENV approach. Later I found other using dotenv package to achieve similar result. source
npm i dotenv
and then
no need to change any config files, the same code apply to both development and production env. Eliminated the need to check NOED_ENV at run time.
I like this method too, the downsides is that you need to add a package for production and import
dotenv/config
in every single file you use it.But the good thing is that you can still use the
VITE_
prefixed variables on the client, and non-prefixed ones in the server in files files hooks and endpoints, without the client know about them.I only think VITE shoud expose non-prefixed ones to server only automatically, and
VITE_
prefixed ones to both server and client, I simply don't know why on earth this isn't the default behaviour.