storybook: Module not found: Error: Can't resolve 'util'

Describe the bug With a fresh build of storybook using react, webpack 5 and yarn PnP, I get an error during build:

ERR! => Failed to build the manager
ERR! Module not found: Error: Can't resolve 'util' in '***/storybook-repro-1/.yarn/__virtual__/@storybook-ui-virtual-0f4525ad5e/4/.yarn/berry/cache/@storybook-ui-npm-6.5.5-c5b3ce8d48-8.zip/node_modules/@storybook/ui/dist/esm'
ERR!
ERR! BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
ERR! This is no longer the case. Verify if you need this module and configure a polyfill for it.
ERR!
ERR! If you want to include a polyfill, you need to:
ERR! 	- add a fallback 'resolve.fallback: { "util": require.resolve("util/") }'
ERR! 	- install 'util'
ERR! If you don't want to include a polyfill, you can use an empty module like this:
ERR! 	resolve.fallback: { "util": false }

To Reproduce Simple repro repository

All I did was:

  1. yarn dlx sb@next repro --pnp
  2. Choose react and react-typescript
  3. yarn add -D webpack @storybook/builder-webpack5 @storybook/manager-webpack5
  4. Change main.ts to include { "core": { "builder": "webpack5" } }
  5. Run yarn build-storybook

System

Environment Info:

  System:
    OS: macOS 12.4
    CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
  Binaries:
    Node: 16.14.2 - /private/var/folders/71/z4j0sg3j26l87c_6nv7y7tz00000gn/T/xfs-6d56c161/node
    Yarn: 3.2.0 - /private/var/folders/71/z4j0sg3j26l87c_6nv7y7tz00000gn/T/xfs-6d56c161/yarn
    npm: 8.9.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 101.0.4951.64
    Edge: 100.0.1185.50
    Firefox: 100.0
    Safari: 15.5

Additional context The same project, but with node-linker: node-modules enabled does not produce the error.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 10
  • Comments: 18 (5 by maintainers)

Commits related to this issue

Most upvoted comments

I had the same error, i just installed util as devDependency yarn add -D util

We also got this error after changing our devDependency from @cypress/code-coverage to 3.10.0.

Before:

npm ls util
+-- @cypress/code-coverage@3.9.12
| `-- @cypress/browserify-preprocessor@3.0.2
|   +-- browserify@16.5.2
|   | +-- assert@1.5.0
|   | | `-- util@0.10.3
|   | `-- util@0.10.4
|   `-- watchify@4.0.0
|     `-- browserify@17.0.0
|       `-- util@0.12.4
`-- @storybook/addon-essentials@6.5.5
  `-- @storybook/core-common@6.5.5
    `-- webpack@4.46.0
      `-- node-libs-browser@2.2.1
        `-- util@0.11.1

After update to @cypress/code-coverage@3.10.0:

npm ls util
`-- @storybook/addon-essentials@6.5.5
  `-- @storybook/core-common@6.5.5
    `-- webpack@4.46.0
      `-- node-libs-browser@2.2.1
        +-- assert@1.5.0
        | `-- util@0.10.3
        `-- util@0.11.1

What seems to work is manually install util as a devDependency, so it can successfully be resolved. We are also using webpack 5 instead of 4. So in our case @storybook/ui/dist/esm has a dependency to util somehow which is not anymore polyfilled by webpack. Maybe this helps for further analysis?

Just ran into this one on the clean install of v7.0.0-rc.4 in the Next.js project after running npx storybook init

Yo-ho-ho!! I just released https://github.com/storybookjs/storybook/releases/tag/v6.5.10-alpha.0 containing PR #18412 that references this issue. Upgrade today to the @prerelease NPM tag to try it out!

npx sb upgrade --prerelease

Jeepers creepers!! I just released https://github.com/storybookjs/storybook/releases/tag/v7.0.0-alpha.2 containing PR #18412 that references this issue. Upgrade today to the @future NPM tag to try it out!

npx sb upgrade --prerelease

Closing this issue. Please re-open if you think there’s still more to do.

I am getting the same, after trying to upgrade storybook to 6.5.5 from 6.4.21. I ran upgrade using npx sb upgrade.