You can use environment variables in Storybook to change its behavior in different “modes”.
If you supply an environment variable prefixed with
STORYBOOK_, it will be available in
process.env when using Webpack, or
import.meta.env when using the Vite builder:
💡 Do not store any secrets (e.g., private API keys) or other types of sensitive information in your Storybook. Environment variables are embedded into the build, meaning anyone can view them by inspecting your files.
You can also access these variables in your custom
<body> using the substitution
%STORYBOOK_X%, for example:
%STORYBOOK_THEME% will become
<link rel="stylesheet" href="%STORYBOOK_STYLE_URL%" />
You can also use
.env files to change Storybook's behavior in different modes. For example, if you add a
.env file to your project with the following:
Then you can access this environment variable anywhere, even within your stories:
Out of the box, Storybook provides a Vite builder, which does not output Node.js globals like
process.env. To access environment variables in Storybook (e.g.,
VITE_), you can use
import.meta.env. For example:
You can also use specific files for specific modes. Add a
.env.production to apply different values to your environment variables.
You can also pass these environment variables when you are building your Storybook with
Then they'll be hardcoded to the static version of your Storybook.
Additionally, you can extend your Storybook configuration file (i.e.,
.storybook/main.js|.ts) and provide a configuration field that you can use to define specific variables (e.g., API URLs). For example:
When Storybook loads, it will enable you to access them in your stories similar as you would do if you were working with an
Storybook allows you to choose the browser you want to preview your stories. Either through a
.env file entry or directly in your
The table below lists the available options:
If you're trying to use framework-specific environment variables (e.g.,
VUE_APP_), you may run into issues primarily due to the fact that Storybook and your framework may have specific configurations and may not be able to recognize and use those environment variables. If you run into a similar situation, you may need to adjust your framework configuration to make sure that it can recognize and use those environment variables. For example, if you're working with a Vite-based framework, you can extend the configuration file and enable the
envPrefix option. Other frameworks may require a similar approach.