storybook: Can't run Storybook with Webpack@5
Describe the bug
When trying to use Storybook in a project that uses webpack@5, and it’s not installed as a direct dependency, but rather as a transitive dependency, you get the following error:
Error: Cannot find module 'webpack/lib/util/makeSerializable.js'
Require stack:
- /Users/saharb/dev/storybook-test/node_modules/@storybook/react-docgen-typescript-plugin/dist/dependency.js
- /Users/saharb/dev/storybook-test/node_modules/@storybook/react-docgen-typescript-plugin/dist/plugin.js
- /Users/saharb/dev/storybook-test/node_modules/@storybook/react-docgen-typescript-plugin/dist/index.js
- /Users/saharb/dev/storybook-test/node_modules/@storybook/react/dist/cjs/server/framework-preset-react-docgen.js
- /Users/saharb/dev/storybook-test/node_modules/@storybook/core-common/dist/cjs/presets.js
- /Users/saharb/dev/storybook-test/node_modules/@storybook/core-common/dist/cjs/index.js
- /Users/saharb/dev/storybook-test/node_modules/@storybook/core-server/dist/cjs/index.js
- /Users/saharb/dev/storybook-test/node_modules/@storybook/core/dist/cjs/server.js
- /Users/saharb/dev/storybook-test/node_modules/@storybook/core/server.js
- /Users/saharb/dev/storybook-test/node_modules/@storybook/react/dist/cjs/server/index.js
- /Users/saharb/dev/storybook-test/node_modules/@storybook/react/bin/index.js
This is due to the fact that react-docgen-typescript-plugin does not install webpack, but relies on it as a peer dependency, and relies on it being version 4, while the webpack installed in the root is version 5:
webpack
├── 4.46.0 (@storybook/builder-webpack4, @storybook/builder-webpack5, @storybook/core-common, @storybook/core-server, @storybook/manager-webpack4, @storybook/manager-webpack5, @storybook/react)
├─┬ @storybook/builder-webpack5
│ └── 5.39.0
├─┬ @storybook/core-client
│ └── 5.39.0
├─┬ @storybook/manager-webpack5
│ └── 5.39.0
└─┬ my-webpack-config-package
└── 5.39.0
To Reproduce
Couldn’t create a repro using npx sb@next repro, and it’s kinda hard to create one manually.
System
Environment Info:
System:
OS: macOS 11.4
CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
Binaries:
Node: 14.16.1 - /var/folders/4v/ysqx3_nn4rs25ffn8zt8kz440000gp/T/fnm_multishell_85829_1623767722434/bin/node
Yarn: 1.22.10 - /var/folders/4v/ysqx3_nn4rs25ffn8zt8kz440000gp/T/fnm_multishell_85829_1623767722434/bin/yarn
npm: 6.14.12 - /var/folders/4v/ysqx3_nn4rs25ffn8zt8kz440000gp/T/fnm_multishell_85829_1623767722434/bin/npm
npmPackages:
@storybook/addon-actions: ^6.3.0-rc.4 => 6.3.0-rc.7
@storybook/addon-essentials: ^6.3.0-rc.4 => 6.3.0-rc.7
@storybook/addon-links: ^6.3.0-rc.4 => 6.3.0-rc.7
@storybook/builder-webpack5: ^6.3.0-rc.4 => 6.3.0-rc.7
@storybook/manager-webpack5: ^6.3.0-rc.4 => 6.3.0-rc.7
@storybook/react: ^6.3.0-rc.4 => 6.3.0-rc.7
Additional context
😶
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 22
- Comments: 24 (10 by maintainers)
I was able to debug what’s going wrong here in Yarn with PnP -
@storybook/reacthas a direct dependency for webpack 4 that causes all transitive dependencies below it to resolve to webpack 4 when they specify webpack 4/5 as peer dependencies. Here’s the relevant part of the dependency tree:With PnP, requests are strictly resolved, so react-doc-gen-typescript always ends up receiving
webpack@4provided by @storybook/react. In NPM or Yarn 1.x, depending on yourpackage-lock.jsonoryarn.lock, these dependencies could be flattened like this:In this case, react-doc-gen-typescript is hoisted and it now retrieves the the project’s webpack version. If you don’t specify
webpack@5as a direct dependency in your project,@storybook/reactand@storybook/builder-webpack5are competing for the version of webpack that is hoisted, so node_modules could look like this:Specifying webpack as a peerDep in
@storybook/reactfor>= 4would resolve this. Alternatively, @iamchanii’s suggestion can be refined slightly withUpdated guide for migrating here.
the resolution for
webpackfor@storybook/reactwasn’t enough for me, aswebpack: 4was defined for several packages. in my case, this helped:In my case, I set resolutions field of package.json (fyi: Yarn Berry)
what happens if you install webpack5 as a direct dev dependency?
Why is
@storybok/reactstill depending on webpack4?@shilman thanks for the quick reply. I have webpack 5 latest,
@storybook/builder-webpack5and@storybook/manager-webpack4in package.json but for some reason it’s still resolving to use the libs for webpack4.we are pulling in quite a few storybook packages so ive removed them all earlier today (hoping there’s something that is no longer necessary). tomorrow i’m going to try reinitializing the project using
npx sb init --builder webpack5if that still doesn’t work I’ll try the resolver overrides because that seems like the least desirable optioni’m trying to upgrade an angular project (that uses storybook) to webpack 5 but storybook keeps pulling in webpack 4 libs and typescript types which cause the build to fail.
looks like
@storybook/core-serverhas:@storybook/builder-webpack4@storybook/manager-webpack4I have a very similar experience. Something seems to be overriding
webpack@5in favour ofwebpack@4and installing that.We have a UI library which uses storybook and another custom package to build itself. Our custom package has as dependency
webpack@5, but storybook has a mix ofwebpack@4andwebpack@5once you use the@storybook/manager-webpack5and@storybook/builder-webpack5. Removing node modules and the package-lock, and doing anpm lsshows something similar to the following:And going to the base
node_modules/webpackshows that it haswebpack@4.46installed. Installing locallywebpack@5works but it is not an intended solution, because that is why we have a custom builder that shares the webpack configs.@shilman installing the
webpack v5as dev dep fixed the issue for meps.: I’m using the
@storybook v6.3.0-rc.11, project with next.js v11, tailwind css v2.2, sass and typescript