Can't run Storybook with Webpack@5
See original GitHub issueDescribe 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
πΆ
Issue Analytics
- State:
- Created 2 years ago
- Reactions:20
- Comments:23 (9 by maintainers)
Top Results From Across the Web
Webpack 5 and Storybook 6 integration throws an error in ...
Basically the error is coming from the marked line in /node_modules/webpack/lib/DefinePlugin.js once running for the first time npm run ...
Read more >Storybook for Webpack 5 - JS.ORG
Add Storybook by running the following command in the root of your project. npx sb init --builder webpack5. Check out the install instructionsΒ ......
Read more >Can't run Storybook with Webpack@5 #15252 - Issuehunt
Can't run Storybook with Webpack@5 #15252 ... This is due to the fact that react-docgen-typescript-plugin does not install webpack , but relies on...
Read more >@storybook/builder-webpack5 - npm
To configure your Storybook to run webpack5 , install @storybook/manager-webpack5 and @storybook/builder-webpack5 as dev dependencies thenΒ ...
Read more >Storybook Webpack Migration - Nx
Nx 12.7 as in combination with Storybook v6.3 doesn't need a custom webpack.config.js which was previously required and auto-generated by Nx.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I was able to debug whatβs going wrong here in Yarn with PnP -
@storybook/react
has 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@4
provided by @storybook/react. In NPM or Yarn 1.x, depending on yourpackage-lock.json
oryarn.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@5
as a direct dependency in your project,@storybook/react
and@storybook/builder-webpack5
are competing for the version of webpack that is hoisted, so node_modules could look like this:Specifying webpack as a peerDep in
@storybook/react
for>= 4
would resolve this. Alternatively, @iamchaniiβs suggestion can be refined slightly withUpdated guide for migrating here.