DEV Community

Discussion on: Environment Variables in SvelteKit and Vercel

Collapse
 
hoishing profile image
Kelvin Ng

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

import dotenv from 'dotenv'
dotenv.config()
let {VAR1, VAR2} = process.env
Enter fullscreen mode Exit fullscreen mode

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.

Collapse
 
danielrios549 profile image
Daniel Rios

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.