DEV Community

Discussion on: Using environment variables in SvelteKit (and Vite)

Collapse
 
p6gb profile image
p6gb

This works fine with the svelte-kit dev script but crashes svelte-kit build with the following error: [rollup-plugin-dynamic-import-variables] Unexpected token (6:265) which points to the line where I use the imported variable in a .svelte file. Any idea why?

Collapse
 
timdeschryver profile image
Tim Deschryver

This is probably because your svelte component includes a style tag?
timdeschryver.dev/blog/environment...

Collapse
 
p6gb profile image
p6gb

That might have been it. I am now adding the env variables to session in a hook:

export function getSession() {
    return {
        API_ENDPOINT: import.meta.env.VITE_API_ENDPOINT
    };
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
timdeschryver profile image
Tim Deschryver

That's smart!

Collapse
 
amaseal profile image
Amaseal

some might still get errors like this even when applying Tim Deshryver's fix, apparently you cant just comment out old code that used this solution

Collapse
 
danawoodman profile image
Dana Woodman

It crashes for you on npm run build? It is working fine for me on a fresh SvelteKit project, maybe you can try from scratch to eliminate variables? Do you have a specific adapter you're using?