storybook: [webpack5] DefinePlugin cannot read property 'get' of undefined
Describe the bug
I cannot build storybook using webpack5. I have the following error message with storybook v6.2.4:
/Users/moroine/Workspace/Nugit/nugit-visualization/node_modules/webpack/lib/DefinePlugin.js:549
const oldVersion = compilation.valueCacheVersions.get(name);
^
TypeError: Cannot read property 'get' of undefined
at /Users/moroine/Workspace/Nugit/nugit-visualization/node_modules/webpack/lib/DefinePlugin.js:549:57
at Array.forEach (<anonymous>)
at walkDefinitionsForValues (/Users/moroine/Workspace/Nugit/nugit-visualization/node_modules/webpack/lib/DefinePlugin.js:545:31)
at /Users/moroine/Workspace/Nugit/nugit-visualization/node_modules/webpack/lib/DefinePlugin.js:571:5
at SyncHook.eval [as call] (eval at create (/Users/moroine/Workspace/Nugit/nugit-visualization/node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:5:1)
at SyncHook.lazyCompileHook (/Users/moroine/Workspace/Nugit/nugit-visualization/node_modules/tapable/lib/Hook.js:154:20)
at Compiler.newCompilation (/Users/moroine/Workspace/Nugit/nugit-visualization/node_modules/@storybook/core-server/node_modules/webpack/lib/Compiler.js:631:26)
at /Users/moroine/Workspace/Nugit/nugit-visualization/node_modules/@storybook/core-server/node_modules/webpack/lib/Compiler.js:667:29
at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/moroine/Workspace/Nugit/nugit-visualization/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:4:1)
at AsyncSeriesHook.lazyCompileHook (/Users/moroine/Workspace/Nugit/nugit-visualization/node_modules/tapable/lib/Hook.js:154:20)
at Compiler.compile (/Users/moroine/Workspace/Nugit/nugit-visualization/node_modules/@storybook/core-server/node_modules/webpack/lib/Compiler.js:662:28)
at /Users/moroine/Workspace/Nugit/nugit-visualization/node_modules/@storybook/core-server/node_modules/webpack/lib/Watching.js:77:18
at _next0 (eval at create (/Users/moroine/Workspace/Nugit/nugit-visualization/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:23:1)
at eval (eval at create (/Users/moroine/Workspace/Nugit/nugit-visualization/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:32:1)
at watchRunHook (/Users/moroine/Workspace/Nugit/nugit-visualization/node_modules/webpack-virtual-modules/index.js:173:5)
at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/moroine/Workspace/Nugit/nugit-visualization/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:28:1)
error Command failed with exit code 1.
I rollback to v6.2.3 and it works (it’s probably caused by dotenv-webpack upgrade.
To Reproduce
Here is my main.js (not the original but this minimal one causes the issue).
module.exports = {
stories: [
'../source/component/**/*.stories.jsx',
'../src/client/**/*.stories.jsx',
],
core: {
builder: 'webpack5',
},
};
System
Environment Info:
System:
OS: macOS 11.2.3
CPU: (8) x64 Apple M1
Binaries:
Node: 12.18.4 - /usr/local/bin/node
Yarn: 1.22.10 - /usr/local/bin/yarn
npm: 6.14.6 - /usr/local/bin/npm
Browsers:
Chrome: 89.0.4389.114
Safari: 14.0.3
npmPackages:
@storybook/addon-actions: 6.2.4 => 6.2.4
@storybook/addon-console: ^1.2.3 => 1.2.3
@storybook/addon-essentials: 6.2.4 => 6.2.4
@storybook/addon-knobs: 6.2.4 => 6.2.4
@storybook/addon-links: 6.2.4 => 6.2.4
@storybook/addon-storyshots: 6.2.4 => 6.2.4
@storybook/addon-storysource: 6.2.4 => 6.2.4
@storybook/addon-viewport: 6.2.4 => 6.2.4
@storybook/builder-webpack5: 6.2.4 => 6.2.4
@storybook/react: 6.2.4 => 6.2.4
Additional context
Given the stack trace, it looks like @storybook/core-server is using webpack4 while root level is webpack5. Here is yarn why webpack
yarn why v1.22.10
[1/4] 🤔 Why do we have the module "webpack"...?
[2/4] 🚚 Initialising dependency graph...
[3/4] 🔍 Finding dependency...
[4/4] 🚡 Calculating file sizes...
=> Found "webpack@5.30.0"
info Has been hoisted to "webpack"
info Reasons this module exists
- Specified in "devDependencies"
- Hoisted from "@storybook#builder-webpack5#webpack"
info Disk size without dependencies: "6.24MB"
info Disk size with unique dependencies: "12.6MB"
info Disk size with transitive dependencies: "24.72MB"
info Number of shared dependencies: 68
=> Found "@storybook/react#webpack@4.46.0"
info This module exists because "@storybook#react" depends on it.
info Disk size without dependencies: "2.41MB"
info Disk size with unique dependencies: "10.59MB"
info Disk size with transitive dependencies: "26.58MB"
info Number of shared dependencies: 111
=> Found "@storybook/core-common#webpack@4.46.0"
info This module exists because "@storybook#addon-storyshots#@storybook#core-common" depends on it.
info Disk size without dependencies: "2.56MB"
info Disk size with unique dependencies: "10.74MB"
info Disk size with transitive dependencies: "26.73MB"
info Number of shared dependencies: 111
=> Found "@storybook/builder-webpack4#webpack@4.46.0"
info This module exists because "@storybook#addon-essentials#@storybook#addon-docs#@storybook#builder-webpack4" depends on it.
info Disk size without dependencies: "2.78MB"
info Disk size with unique dependencies: "10.96MB"
info Disk size with transitive dependencies: "26.95MB"
info Number of shared dependencies: 111
=> Found "@storybook/core-server#webpack@4.46.0"
info This module exists because "@storybook#addon-storyshots#@storybook#core#@storybook#core-server" depends on it.
info Disk size without dependencies: "2.75MB"
info Disk size with unique dependencies: "10.93MB"
info Disk size with transitive dependencies: "26.92MB"
info Number of shared dependencies: 111
✨ Done in 1.60s.
┆Issue is synchronized with this Asana task by Unito
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 12
- Comments: 26 (7 by maintainers)
Commits related to this issue
- Add dotenv-webpack to work around storybookjs/storybook#14403 https://github.com/storybookjs/storybook/issues/14497 and https://github.com/storybookjs/storybook/issues/14403 tl;dr: Happo stopped wor... — committed to VandyHacks/vaken by leonm1 3 years ago
- Add dotenv-webpack to work around storybookjs/storybook#14403 (#821) https://github.com/storybookjs/storybook/issues/14497 and https://github.com/storybookjs/storybook/issues/14403 tl;dr: Happo st... — committed to VandyHacks/vaken by leonm1 3 years ago
- https://github.com/storybookjs/storybook/issues/14497#issuecomment-814813297 — committed to wscherphof/fastify-htmxample by wscherphof 2 years ago
- fix: Add dot env property based on thread for issue with webpack 5 https://github.com/storybookjs/storybook/issues/14497 — committed to WPMedia/arc-themes-components by JackHowa 2 years ago
If someone is still looking around for a solution, here is what worked for me :
“optimization”: { “styles”: false, “scripts”: false, “fonts”: false }
This fixed the warning - “Cannot read property ‘minify’ of undefined”
P.S: I followed all these steps while on storybook 6.2.7 version
OK, I’ve been reading a lot of different threads here and it seems what solved it for me was adding
dotenv-webpackto dev dependencies (6.0.4 was enough) andhtml-webpack-plugin. Without the second one, I was just getting a different error (about snapshots). Now I seem to have moved past this error into into some unrelated ones.postcsswasn’t working. I followed the migration guide but this proved to not be enough. Step 3 from this post worked.Edit: deleted last part of post - it was about an error with SCSS not being recognised but I hadn’t updated my
main.jsto account for changes towebpack.config.jswith the webpack 5 upgrade. Nothing worth sharing here.Storybook is running now! 🎉
@matttk It looks like you have similar issue what explained here: https://gist.github.com/shilman/8856ea1786dcd247139b47b270912324#gistcomment-3704208
Mainly what solved the issue on my end is installing
"dotenv-webpack": "^7.0.2"asnpm install -D dotenv-webpack.This SO answer gave me the idea, also see the reason for that here: https://github.com/storybookjs/storybook/issues/14403#issuecomment-814209504
Not sure if this is related to my problems, but I can reproduce errors with 6.2.9:
I have a solid repro case now, thank you for your debugging info!
FYI: We have a monorepo. We only use WebPack for Storybook but for reasons I also included
yarn whyoutput.Validation that 6.2.4 is installed:
node_modules/webpack/package.json=> 5.30.0yarn why:
node_modules/dotenv-webpack/package.json=> 6.0.4If you need more info, let me know 😃
Didn’t help, unfortunately. I never had a
storybook-staticfolder because I never runbuild-storybook. I only usenpm run storybook.Maybe my problem looks the same but is different…
ZOMG!! I just released https://github.com/storybookjs/storybook/releases/tag/v6.2.5 containing PR #14501 that references this issue. Upgrade today to the
@latestNPM tag to try it out!Closing this issue. Please re-open if you think there’s still more to do.
#14576 will probably solve this
This one saved me! I upgraded some “inherited” repos and was struggling with this issue. Thanks!