builder-vite: Builder breaks with package managers that don't hoist (e.g. pnpm)
I recently tried to upgrade from version 0.0.8 to 0.0.10 on my repo, which is using pnpm as its package manager.
pnpm does not hoist dependencies (so, for example, storybook’s dependencies are not available in the project’s top level node_modules).
When starting a project with storybook-builder-vite and pnpm, we see this error: Error: Failed to resolve force included dependency: airbnb-js-shims, which is one of the forced optimized dependencies here: https://github.com/eirslett/storybook-builder-vite/blob/main/packages/storybook-builder-vite/optimizeDeps.js
I’m confused because:
- this used to work in 0.0.8 and
airbnb-js-shimswas in that list there require.resolve("airbnb-js-shims")shouldn’t work from this file in pnpm, based on my understanding of node module resolution and pnpm. It is resolving, so something is wrong with my understanding
Manually installing airbnb-js-shims fixes this error, but it is replaced with the next one down the list.
Reproduction here: https://github.com/Pinpickle/storybook-builder-vite-0.0.10-pnpm-bug (you can run it under pnpm and yarn to see it breaking and working)
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 47
- Comments: 50 (13 by maintainers)
Commits related to this issue
- Do not optimize storybook packages (#323) This PR removes many of the storybook packages from pre-bundling, which should save a little bit of time in startup, and will 🤞 work a little better in pnpm... — committed to storybookjs/builder-vite by IanVS 2 years ago
- chore: Drop `yarn2nix` and switch to `pnpm`. This is a wide-ranging change in which we drop `yarn2nix` (and Yarn itself) in favor using `pnpm` directly, with no Nix layer in-between the `pnpm` lockfi... — committed to hackworthltd/primer-app by dhess 2 years ago
- chore: Drop `yarn2nix` and switch to `pnpm`. This is a wide-ranging change in which we drop `yarn2nix` (and Yarn itself) in favor using `pnpm` directly, with no Nix layer in-between the `pnpm` lockfi... — committed to hackworthltd/primer-app by dhess 2 years ago
- chore: Drop `yarn2nix` and switch to `pnpm`. This is a wide-ranging change in which we drop `yarn2nix` (and Yarn itself) in favor using `pnpm` directly, with no Nix layer in-between the `pnpm` lockfi... — committed to hackworthltd/primer-app by dhess 2 years ago
- chore: Drop `yarn2nix` and switch to `pnpm`. This is a wide-ranging change in which we drop `yarn2nix` (and Yarn itself) in favor using `pnpm` directly, with no Nix layer in-between the `pnpm` lockfi... — committed to hackworthltd/primer-app by dhess 2 years ago
- chore: Drop `yarn2nix` and switch to `pnpm`. This is a wide-ranging change in which we drop `yarn2nix` (and Yarn itself) in favor using `pnpm` directly, with no Nix layer in-between the `pnpm` lockfi... — committed to hackworthltd/primer-app by dhess 2 years ago
- chore: Drop `yarn2nix` and switch to `pnpm`. This is a wide-ranging change in which we drop `yarn2nix` (and Yarn itself) in favor using `pnpm` directly, with no Nix layer in-between the `pnpm` lockfi... — committed to hackworthltd/primer-app by dhess 2 years ago
- feat: Added Storybook with sample story and tailwindcss support Added storybook using pnpm with set hoist pattern - see [https://github.com/storybookjs/builder-vite/issues/55#issuecomment-1273443058] — committed to Indeedornot/SvelteToDo by Indeedornot 2 years ago
- feat: Added Storybook with sample story and tailwindcss support Added storybook using pnpm with set hoist pattern - see [https://github.com/storybookjs/builder-vite/issues/55#issuecomment-1273443058] — committed to Indeedornot/SvelteToDo by Indeedornot 2 years ago
- feat: Added Storybook with sample story and tailwindcss support Added storybook using pnpm with set hoist pattern - see [https://github.com/storybookjs/builder-vite/issues/55#issuecomment-1273443058] — committed to Indeedornot/SvelteToDo by Indeedornot 2 years ago
- Fixed Failed to resolve import. see:storybookjs/builder-vite/issues/55 — committed to fufuShih/storybook-react-typescript-vite-mui by fufuShih 2 years ago
- Workaround for https://github.com/storybookjs/builder-vite/issues/55 — committed to seanchas116/uimix by seanchas116 a year ago
- Add : .npmrc for issue with pnpm, vite and storybook. https://github.com/storybookjs/builder-vite/issues/55 — committed to KuramaBiju/eteam_frontend_storybook by KuramaBiju a year ago
- build: don't hoist any storybook packages to avoid vite hoisting issues, storybookjs/builder-vite/issues/55 — committed to nerdyman/react-compare-slider by nerdyman a year ago
FWIW, we are working on improved pnpm support in 7.0, and our goal is to not require any hoisting or workarounds.
I found the offending commit: https://github.com/eirslett/storybook-builder-vite/commit/dde6b5fbe56c3fb24ff53c564e61a334984903cc, AKA #28
Basically, as the root used to be inside
storybook-builder-vite, the resolutions inoptimizeDepsare relative to the root. Now that the root is in the project itself, these resolutions aren’t necessarily correct.So we want some optimized deps/import to be relative to storybook-builder-vite and the rest be relative to the project root.
Not sure how to accomplish that, though 😅
In the meantime, I’ve added this to my
viteFinalto basically undo that commit:And it works!
This single line in .npmrc fixed my problems with storybook and pnpm:
I do not want to “shamefully” hoist modules as it was suggested before since I would like to avoid phantom dependencies. Because of this, I also went for manual package installations, just like @TomABrook did. Another way to provide these missing dependencies without polluting the actual
package.jsoncould be to use pnpm’sreadPackagehook to inject them:My company is migrating our monorepo to pnpm workspaces and this has been one of the last blockers for us. The vite final workaround posted above doesn’t seem to work when the storybook is part of a workspaces, however I have managed to get it working by adding the following dev dependencies:
I’ve been doing some experiments to avoid some of the pre-bundling that we’ve been doing up to now. I’m hopeful that it’ll improve the situation for pnpm users. I’ll report back here if/once I have something more definite, but wanted to let y’all know I’m working on it.
@eirslett that works, yep
This makes sense, given that it’s consistent with yarn/npm’s hoisting behaviour. It’s not really a viable workaround, as removing hoisting is one of the benefits of pnpm.
Two years later I’ve finally tried this again and it works out-of-the-box with my Vite and pnpm setup. Storybook team: thanks for the hard work improving compatibility!
Hi! It’s still not working for me after running
npx sb@next upgrade --prerelease. I’m getting the following error:I’m running
pnpmin a monorepo and trying to get storybook running for one of the subpackages. In a pnpm monorepo the actualnode_modulesassets are located in the root folder. Maybe that has something to do with the issues? Here are the dependencies:Using
public-hoist-pattern[]=*storybook*as suggested by others does work.Just to attach some complexity to this issue, if you are using pnpm with (rushjs)[https://rushjs.io/pages/intro/get_started/] you don’t have the option to use --shamefully-hoist
This seems to have been resolved with Storybook 7 release for us.
@RIP21 It works perfectly fine for me, have a look here https://github.com/kaminskypavel/fullpower-stack/tree/master/packages/ui
@IanVS did a great job !👏
🎉 Thanks a lot @srflp for this solution ! works perfectly without clogging package.json or vite config.optimizeDeps.include with a bunch of storybook packages Don’t forget to run
pnpm installto update node_modules structure after your changesThis solution to change root appeared to work for me in the first time but was actually breaking
@storybook/addon-postcss. The CSS file that i was importing inpreview.tsxwith@tailwinddirectives was picked up but tailwind styles were not applied. HMR on modification of a componentclassnamedid not trigger an update of the CSS file too.That might be a suitable work around, but not a real solution 🤔
https://pnpm.io/npmrc#public-hoist-pattern
In case it helps anyone trying to get pnpm working with
@storybook/vue3, I put together a repo with a starter project using Vue 3 + Vite + TS + pnpm + Storybook and the changes that got it all working for me (forked from @IanVS’s repo above – thank you!): https://github.com/jdk2pq/vite-storybook-pnpm-vuejust tested it, and it seems to work. unfortunately, it creates a new issue related to deduping
@storybook/client-api. the solution here seems to help thoughThanks for the info @Dschungelabenteuer and FYI @omarelb, this is also working and a little less generic:
Thank you for this @jdk2pq ! I was able to get this done following your process.
I’m not a pnpm expert by any means, but I’ve been working on getting the original reproduction above working, and after a lot of hacking around, I think I’ve done it. I pushed up the changes to a new repo: https://github.com/IanVS/vite-storybook-pnpm-shame
This was a combination of adding a lot of new
optimizeDeps.includethat are sub-dependencies (which we can add to our own list in this project so others don’t need to), but a few of them required adding the dependencies directly to the repo, which I don’t understand and don’t like. I’m not sure if this is a vite bug, or weird pnpm thing, or what. But at least it seems possible to get this working without hoisting.@fdc-viktor-luft I rolled my own, yeah. I only used storybook for development so I just needed an easy way to look at components with specific props.
My code is pretty unique to my setup, but the main magic is using glob imports, i.e.
import.meta.glob("./**/*.stories.tsx")@Pinpickle Your workaround seems not to work within
pnpmworkspaces. Transforming this repository intopnpmworkspace and adding the above workaround allows to reproduce the following issue. (See https://github.com/eirslett/storybook-builder-vite/pull/92)When running
cd packages/example-react && pnpm storybookyou get the following error:Do you have any idea for an easy workaround?
@IanVS sorry i never got around to working on a minimal repro, but i did return to trying to upgrade my yarn v3.x monorepo to storybook v7.x and the vite builder, and i wound up getting everything to work. it turned out that the errors i was seeing were mostly just symptoms of missing dependencies. in the end, i just needed to thoroughly configure the
packageExtensionsin my.yarnrc.ymlfile and use thegetAbsolutePathhelper in my.storybook/main.tsfile. here’s all of the necessary pieces (i’m using v7.5.0-alpha.5 and react-docgen v6.x):storybook workspace’s
package.json:root
.yarnrc.yml:.storybook/main.ts:I think it is in some part due to Storybook and it’s use of peerDependencies. I hear Storybook 7.0 is planning to address this.
@Pinpickle Thanks for your response.
Do you have a working alternative or did you just do a simple “storybook” yourself instead?
@fdc-viktor-luft that doesn’t sound good! Truth be told, I’ve stopped using storybook because even with this working, it was still painfully slow.
I’m not sure why this error would come up now, when it didn’t come up before. I think the real solution here would be to fix this builder, so the root can correctly be set to the project root (and React be installed).
On another note, Vite’s support of nested
optimizeDeps.include(landed in 2.5.2, https://github.com/vitejs/vite/pull/4634) should make this easier to fix properly.