npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

vite-plugin-sveltekit-env-dynamic-public

v1.1.1

Published

Vite plugin to shim SvelteKit $env/dynamic/public for Storybook

Downloads

15

Readme

vite-plugin-sveltekit-env-dynamic-public

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.

The problem

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

Alternate Solution (you probably don't need this plugin)

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 = {};

What this plugin does

  • Captures imports of "$env/dynamic/public"
  • Replaces them with a virtual module that exports:
export const env = { /* your overrides */ }

By default, the exported env is {}.

Why is this needed?

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.

Caveats

  • This is a Storybook-only workaround. Don’t add it to your actual SvelteKit app build.
  • It only targets "$env/dynamic/public". If you need additional SvelteKit shims, consider opening an issue or PR.

Install

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

Usage (Storybook + SvelteKit)

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;

Providing static env overrides

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

Options

env

Type:

Record<string, string | number | boolean | null | undefined>

Notes:

  • Values are serialized with JSON.stringify()
  • undefined keys are dropped (standard JSON behavior)

License

MIT