A tiny Vite plugin that makes SvelteKit components using $env/dynamic/public render correctly in Storybook.
Storybook claims to support SvelteKit’s $env/dynamic/public, but in practice, stories will throw an error when a component imports it (in both dev and static/build modes). This plugin fixes that by intercepting the import and replacing it with a virtual module that exports a static env object.
This Svelte component is enough to trigger the issue in Storybook:
<script lang="ts">
import { env } from '$env/dynamic/public';
const label = env.PUBLIC_BUTTON_LABEL ?? 'Hello World';
</script>
<button>{label}</button>
The error you’ll see looks like:
can't access property "env", globalThis.__sveltekit_dev is undefined
The component failed to render properly, likely due to a configuration issue in Storybook.
@http://localhost:6006/@id/__x00__virtual:env/dynamic/public:1:20
After creating this plugin, I discovered this simpler method by alexhladun-orennia.
Update your .storybook/main.ts to override Vite's resolve.alias config:
import type { StorybookConfig } from '@storybook/sveltekit'
import { mergeConfig } from 'vite'
const config: StorybookConfig = {
// ... existing config ..
async viteFinal(config) {
return mergeConfig(config, {
resolve: {
alias: {
'$env/dynamic/public': import.meta.resolve('./env.public.ts'),
},
},
}),
}
And then create the .storybook/env.public.ts file:
// Make storybook happy, as '$env/dynamic/public' is not defined in storybook
// You can also override env vars here for testing.
export const env = {};
"$env/dynamic/public"export const env = { /* your overrides */ }
By default, the exported env is {}.
SvelteKit’s $env/dynamic/public is implemented via runtime behavior that depends on SvelteKit globals. Storybook doesn’t provide the same runtime, so importing the module crashes at evaluation time. This plugin avoids the crash by replacing the module with a stable, static implementation for Storybook.
"$env/dynamic/public". If you need additional SvelteKit shims, consider opening an issue or PR.npm i -D vite-plugin-sveltekit-env-dynamic-public
pnpm add -D vite-plugin-sveltekit-env-dynamic-public
yarn add -D vite-plugin-sveltekit-env-dynamic-public
In .storybook/main.ts, add the plugin only for Storybook via the viteFinal method:
import type { StorybookConfig } from '@storybook/sveltekit';
import { sveltekitEnvDynamicPublic } from 'vite-plugin-sveltekit-env-dynamic-public';
const config: StorybookConfig = {
/*
* ...your existing storybook config...
*/
/*
* Inject the `sveltekitEnvDynamicPublic` vite plugin
* to ensure that components can safely reference "$env/dynamic/public"
*/
async viteFinal(config) {
return {
...config,
plugins: [
sveltekitEnvDynamicPublic(),
...(config.plugins ?? []),
],
};
},
};
export default config;
If your components read env.PUBLIC_* values, you can set them in Storybook:
sveltekitEnvDynamicPublic({
env: {
PUBLIC_BUTTON_LABEL: 'Custom Button Label',
PUBLIC_FEATURE_ENABLED: true,
},
}),
This makes the following work in stories:
import { env } from '$env/dynamic/public';
env.PUBLIC_BUTTON_LABEL; // "Custom Button Label"
env.PUBLIC_FEATURE_ENABLED; // true
envType:
Record<string, string | number | boolean | null | undefined>
Notes:
JSON.stringify()undefined keys are dropped (standard JSON behavior)MIT