question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't use SvelteKit $app stores in Stories

See original GitHub issue

I’m trying to get a basic story working using SvelteKit + Storybook using the Vite builder. This story uses the page store from$app/stores which is imported into the Nav.svelte file as below:

import { page } from "$app/stores";

In my .storybook/main.js file, I have this config including the alias for the $app

const preprocess = require("svelte-preprocess");
const autoprefixer = require("autoprefixer");
const path = require("path");
const { mergeConfig } = require("vite");

module.exports = {
  stories: [
    "../src/**/*.stories.mdx",
    "../src/**/*.stories.@(js|jsx|ts|tsx|svelte)",
  ],
  addons: [
    "@storybook/addon-links",
    "@storybook/addon-essentials",
    "@storybook/addon-svelte-csf",
  ],
  framework: "@storybook/svelte",
  svelteOptions: {
    preprocess: preprocess({
      sourceMap: true,
      scss: {
        includePaths: ["src"],
        prependData: `@import 'src/styles/variables.scss';`,
      },
      postcss: {
        plugins: [autoprefixer()],
      },
      aliases: [["ts", "typescript"]],
    }),
  },
  core: {
    builder: "@storybook/builder-vite",
  },
  async viteFinal(config, { configType }) {
    config.resolve.alias = {
      $app: path.resolve("./.svelte-kit/runtime/app"),
    };
    console.log(config);
    return config;
  },
};

but when I try to view the Nav story, I get the below error

Cannot read properties of undefined (reading 'page')
TypeError: Cannot read properties of undefined (reading 'page')
    at getStores (http://localhost:6006/.svelte-kit/runtime/app/stores.js:23:22)
    at Object.subscribe (http://localhost:6006/.svelte-kit/runtime/app/stores.js:45:17)
    at subscribe (http://localhost:6006/node_modules/.vite-storybook/chunk-R7OMEHAZ.js?v=3466fb57:61:23)
    at component_subscribe (http://localhost:6006/node_modules/.vite-storybook/chunk-R7OMEHAZ.js?v=3466fb57:70:32)
    at instance (http://localhost:6006/src/components/Nav/Nav.svelte:121:2)
    at init (http://localhost:6006/node_modules/.vite-storybook/chunk-R7OMEHAZ.js?v=3466fb57:1686:23)
    at new Nav (http://localhost:6006/src/components/Nav/Nav.svelte:137:3)
    at createProxiedComponent (http://localhost:6006/node_modules/svelte-hmr/runtime/svelte-hooks.js:266:9)
    at new ProxyComponent (http://localhost:6006/node_modules/svelte-hmr/runtime/proxy.js:239:20)
    at new Proxy<Nav> (http://localhost:6006/node_modules/svelte-hmr/runtime/proxy.js:346:11)

Has anyone been able to successfully set an alias to the $app/stores?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
IanVScommented, Apr 26, 2022

I’m going to close this out since it doesn’t sound like a builder-vite bug, but please report back @kyleshepherd if you think it is, or if you find a good solution that you think might be useful to others.

1reaction
kyleshepherdcommented, Apr 7, 2022

I did try it with Webpack but was getting a similar error.

I’ll try and do some more digging now knowing that the alias is working itself and see if I can figure anything out. Thank you for looking into it!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to test files with $app/stores import after migrating to ...
I haven't tested with runtime modules like $app , but here is a workable solution using vi.mock in order to test Header.svelte in...
Read more >
for vite svelte users, $app/stores not found. Where to get it?
I tried to npm install svelte-kit but empty folder. I tried sveltekit template, it can work. but vite -> svelte cannot find $app/stores....
Read more >
Fix SvelteKit app/navigation for Storybook and some Github ...
Your browser can't play this video. ... Fix SvelteKit app /navigation for Storybook and some Github actions build improvements.
Read more >
SvelteKit $app/stores - YouTube
In this video, we talk about one of the special modules from SvelteKit, $ app / stores.What are they for, how do you...
Read more >
Working with Svelte stores - Learn web development | MDN
You'll need a terminal with node and npm installed to compile and build your app. Objective: Learn how to use Svelte stores ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found