question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Does not work with `@nrwl/react/plugins/storybook` addon

See original GitHub issue

Describe the bug

Storybook gives the error:

ModuleBuildError: Module build failed (from ./node_modules/storybook-addon-next/dist/images/next-image-loader-stub.js):
TypeError: Cannot read properties of undefined (reading 'replace')
    at Object.nextImageLoaderStub (/Users/anatoo/Workspace/xxx/xxx/node_modules/storybook-addon-next/dist/images/next-image-loader-stub.js:7:75)
    at processResult (/Users/anatoo/Workspace/xxx/xxx/node_modules/webpack/lib/NormalModule.js:758:19)
    at /Users/anatoo/Workspace/xxx/xxx/node_modules/webpack/lib/NormalModule.js:860:5
    at /Users/anatoo/Workspace/xxx/xxx/node_modules/loader-runner/lib/LoaderRunner.js:400:11
    at /Users/anatoo/Workspace/xxx/xxx/node_modules/loader-runner/lib/LoaderRunner.js:252:18
    at runSyncOrAsync (/Users/anatoo/Workspace/xxx/xxx/node_modules/loader-runner/lib/LoaderRunner.js:156:3)
    at iterateNormalLoaders (/Users/anatoo/Workspace/xxx/xxx/node_modules/loader-runner/lib/LoaderRunner.js:251:2)
    at /Users/anatoo/Workspace/xxx/xxx/node_modules/loader-runner/lib/LoaderRunner.js:224:4
    at /Users/anatoo/Workspace/xxx/xxx/node_modules/webpack/lib/NormalModule.js:834:15
    at Array.eval (eval at create (/Users/anatoo/Workspace/xxx/xxx/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:12:1)
    at runCallbacks (/Users/anatoo/Workspace/xxx/xxx/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:27:15)

Your minimal, reproducible example

None

Steps to reproduce

  1. Add @nrwl/react/plugins/storybook addon to “.storybook/main.js”
  2. Run storybook but it just fail with the above error

Expected behavior

Storybook start normally

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: macOS

storybook-addon-next version

v1.6.7

Additional context

@nrwl/react/plugin/storybook add the below webpack loader rule.

{
  test: /\.(bmp|png|jpe?g|gif|webp|avif)$/,
  type: 'asset',
  parser: { dataUrlCondition: { maxSize: 10000 } }
} 

storybook-addon-next 's configureStaticImageImport function refer generator.filename option of the rule.

  const assetRule = rules?.find(
    rule =>
      typeof rule !== 'string' &&
      rule.test instanceof RegExp &&
      rule.test.test('test.jpg')
  ) as RuleSetRule
  assetRule.test = /\.(apng|eot|otf|ttf|woff|woff2|cur|ani|pdf)(\?.*)?$/
  rules?.push({
    test: /\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$/i,
    issuer: { not: /\.(css|scss|sass)$/ },
    use: [
      {
        loader: require.resolve('./next-image-loader-stub'),
        options: {
          filename: assetRule.generator?.filename // <<----
        }
      }
    ]
  })
  rules?.push({
    test: /\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$/i,
    issuer: /\.(css|scss|sass)$/,
    type: 'asset/resource',
    generator: {
      filename: assetRule.generator?.filename // <<----
    }
  })

https://github.com/RyanClementsHax/storybook-addon-next/blob/main/src/images/webpack.ts#L15-L41

As a result, the generator.filename option is undefined and the storybook is broken.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:6
  • Comments:15

github_iconTop GitHub Comments

3reactions
mandarinicommented, Sep 30, 2022

We are already looking into it! Thank you all for reporting!

3reactions
ixahmedxicommented, Sep 29, 2022

I have a related issue. Nx mono repository with Next.js frontend and Storybook. Everything is mostly default settings.

storybook-addon-next 1.6.9

TypeError: Cannot set properties of undefined (setting 'test')
at configureStaticImageImport (/Users/exsesx/Space/s-projects/tso_v3/tso/node_modules/.pnpm/storybook-addon-next@1.6.8_4ezhcqr4l5iwrkp7cpn27pk7rm/node_modules/storybook-addon-next/dist/images/webpack.js:21:20)

This line throws the error because assetRule is undefined:

https://github.com/RyanClementsHax/storybook-addon-next/blob/104b02b997a10a283d069d1b6639dce02f903467/src/images/webpack.ts#L21

I’ve fixed this by downgrading @nrwl/react to 14.7.5

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrate to the Nrwl React Storybook Preset - Nx
The Nx Plugin for Storybook contains executors and generators for allowing your workspace to use the powerful Storybook integration testing & documenting ...
Read more >
is missing from the TypeScript compilation. Please make sure ...
I've been googling for quite a while and reading through several threads, but so far nothing has been able to help me. I...
Read more >
Speed up Storybook with Vite and SWC — with the help of Nx
Vite is a build tool that ensures faster load times, faster updates, ... rootMain.addons, '@nrwl/react/plugins/storybook'], webpackFinal: async (config, ...
Read more >
@nrwl/storybook - npm
Nx is a next generation build system with first class monorepo support and powerful integrations. This package is a Storybook plugin for Nx....
Read more >
Vite - Storybook
Storybook is a frontend workshop for building UI components and pages in ... If you're working with Svelte's Vite plugin ( vite-plugin-svelte ),...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found