storybook: [Bug]: vite builder fails trying to optimize files in the storybook cache folder
Describe the bug
Often after switching branches I encounter the following error type when starting Storybook (causing it to fail to load)
The file does not exist at "<PROJECT_PATH>/node_modules/.cache/storybook/a6871e0c26d33fcbda0875414a230db7277d874a67a0ffb34bdf44755e21997a/sb-vite/deps/chunk-WMAVEDOJ.js?v=78430ec4" which is in the optimize deps directory.
The dependency might be incompatible with the dep optimizer. Try adding it to `optimizeDeps.exclude`.
Deleting the .cache folder or even the entire node_modules folder does not resolve this.
However, there are a couple things that resolve it (until it occurs again):
- Changing / upgrading the Storybook version (any version bump will do)
- Changing the Storybook
main.tsconfig (it seems any change will do) - Making a fresh clone of the repo
However, reverting the changes above causes the problem to re-emerge. The one thing I could think of here is that there is some caching (by Vite or Storybook) happening globally or at least outside the project folder, but I could not find any documentation on this
Reproduction link
Sorry I don’t have any, it might be project specific
Reproduction steps
I don’t have reproduction steps because it happens fairly randomly. It happens typically when switching between feature branches and then back.
System
Storybook Environment Info:
System:
OS: macOS 14.4.1
CPU: (8) arm64 Apple M1
Shell: 5.9 - /bin/zsh
Binaries:
Node: 18.18.1 - ~/.local/state/fnm_multishells/98494_1720679783884/bin/node
Yarn: 4.1.1 - /opt/homebrew/bin/yarn <----- active
npm: 9.8.1 - ~/.local/state/fnm_multishells/98494_1720679783884/bin/npm
Browsers:
Safari: 17.4.1
Additional context
Storybook version: 8.1.11
Builder: @storybook/react-vite
Project is monorepo with Yarn 4 workspaces
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 28
- Comments: 27 (9 by maintainers)
Can this issue be reopened as a clear solution was not reached?
I just had the same issue but with another project and without a dedicated frontend client like storybook here. For me disabling the cache on my browser and then reloading solved it. Not sure how to apply this to storybook though, I am not familiar with this project
My error is:
And I add this to .storybook/main.ts:
This works for me
I’m afraid we need to close this issue for now, since we can’t take any action without the requested reproduction or additional information. But please don’t hesitate to open a new issue if the problem persists – we’re always happy to help. Thanks so much for your understanding.
As described in the issue, in this case deleting the
.cachefolder or entirenode_modulesdoes not resolve it. That’s the strange thing, because normally something like that would resolve it, and to me it implied some global caching happening somewhere.I’ve been having this issue intermittently in my local repo but never on CI. Clearing node_modules and/or the .cache directory specifically does nothing to resolve things.
I noticed that switching browsers or going incognito results in no issues so I assume the issue is something browser cache related. I tried “clear site data” in chrome but it didn’t work, but unchecking “disable cache” on the network tab, then reloading, then re-checking “disable cache” solved the issue in my original browser profile.
@vanessayuenn I understand but the issue occurs in a seemingly unpredictable manner and seems quite specific to this project. The project has quite a few dependencies that are internal packages, so that doesn’t help. The Vite error is not very useful as it is not mentioning what dependency it relates to, only that some cached chunk is missing.
optimizeDeps.excludewith various dependencies, but the issue disappears temporarily whenever the config is changed. This makes it unclear if it was the right package. Actually I now put the following which so far seems to fix the entire issue by always “changing” the config.optimizeDeps.includeandoptimizeDeps.force, but all without success.I’m just wondering what could explain the mechanism that any change to the
optimizeDepssetting temporarily fixes the problem, and so does any storybook version bump.Thanks @timmevandermeer for your workaround. We already added
'node_modules/.cache/storybook'toconfig.optimizeDeps.exclude, but right now I faced a situation where this didn’t helped anymore. Clearing any kind of caches, re-installing deps, etc. also didn’t work. Onlyconfig.optimizeDeps.exclude.push(`fake-dep-${Math.random()}`)made it possible for me to start Storybook again.@oleksandr-danylchenko yes that has been my mitigation too, see here: https://github.com/storybookjs/storybook/issues/28542#issuecomment-2225296609
Another thing that helped - explicit storybook cache exclusion from the Vite’s optimization:
UPD: After updating one of the dependencies in
package.json- it didn’t helpI’m not using storybook, but I’m getting the same message. I posted at https://github.com/vitejs/vite/discussions/17738 (not a bug report, because they require a minimal reproduction)
To address the issue of Vite builder failing due to optimizing files in the Storybook cache folder, follow these steps:
Modify
optimizeDepsConfiguration:/code/builders/builder-vite/src/optimizeDeps.ts.optimizeDeps.excludearray.Update Vite Configuration:
/code/builders/builder-vite/src/vite-config.ts.optimizeDepsconfiguration is merged correctly.Rebuild and Restart:
This should prevent Vite from attempting to optimize the problematic dependencies in the Storybook cache folder.
References
/code/builders/builder-vite/src/optimizeDeps.ts /code/builders/builder-vite/src/vite-config.ts /code/builders/builder-vite/src/index.ts /code/builders/builder-vite
About Greptile
This response provides a starting point for your research, not a precise solution.
Help us improve! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.
Ask Greptile · Edit Issue Bot Settings
I ran into a similar/same issue. Nuking
node_modules/.cache,node_modules/.vite,node_modules/.vite-tempdid not help. What did help was clearing the browser cache 😕I’ve re-opened this as it seems to be extremely annoying and affects a bunch of people. However, in order to fix it we need a good reproduction. If anybody who’s here can help with that, it would be much appreciated!
I switch branches a lot that seems to mess up Storybook’s caches and causes Storybook to not work at all. Deleting
node_modules/.cachedoes not help.I’m getting e.g.
The only way I’ve managed to fix this is by adding the following to
main.tsand deletingnode_modules/.cache:If the above does not work, I have to delete the whole repo and clone it again to make it work. Sometimes even cloning again does not work.
Another thing that helps me with occasional cache mismatches is changing the:
to something absurd like:
Then when I start the Storybook, it somehow revalidates the Vite’s cache and runs all the stories properly. Then I can go back to the adequate path, restart, and enjoy my day till the next caching issue 🪄
For me, deleting
node_modules/.cachefixes it. I only have the issue when running storybook. I can build and run the application without issue. If I introduce a new export from an internal workspace package the problem comes back.The same issue started happening to me today!
The issues seem to happen only in the dev branch, but it doesn’t contain any changes to the SB or Vite configs 🤯
UPD:
Bumping to:
helped to resurrect the Storybook, but I’m not sure about the longevity of such a solution…