How can I use Vite env variables in svelte.config.js?

Viewed 487

For example, in .env.local, I have

VITE_BASE=/sveltekit-ghpages

I can access this variable inside src using import.meta.env.VITE_BASE. But inside svelte.config.js, it doesn't work.

How can I use Vite env variables in svelte.config.js?

1 Answers

Option 1:

If you are already using dotenv if your project then you can do this in your svelte.config.js file


import 'dotenv/config';

console.log("config", process.env.FOO)

Option 2:

If your node script is overloaded with environment variables, for example


...

"scripts": {
        "build": "FOO=BAR vite build"
}

...

then you can use console.log( process.env.FOO) without having to install dotenv svelte.config.js file

Related