storybook: [Bug]: Next.js module transpilation is not supported

Describe the bug

Despite the Next.js + Storybook addon description claims “What Next.js supports out of the box, this addon makes possible in Storybook” the Next.js Built-in module transpilation doesn’t seem to be working.

I have some un-transpiled ES6 packages that previously used next-transpile-modules and now use the built-in the Next.js configuration. These work fine in Next.js but fail in Storybook after migrating to v7.

Previously I was using the following Webpack custom configuration

webpackFinal: async (config) => {
  config.module.rules[0].exclude = [/node_modules\/(?!@my-scope).*/]
  return config
}

The Next.js addon doesn’t seem to detect the transpilePackages attribute from the next.config.js nor does the custom Webpack rule work.

To Reproduce

  1. Install a dependency with un-transpiled ES6 code.
  2. Add this dependency to be transpiled into Next.js as per https://nextjs.org/blog/next-13-1#built-in-module-transpilation-stable
  3. Verify that Next.js runs correctly.
  4. Verify that Storybook fails by not transpiling the dependency. You will see an error similar to “You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.”

System

System:
  OS: macOS 13.2
  CPU: (8) arm64 Apple M1 Pro
Binaries:
  Node: 16.18.1 - /opt/homebrew/opt/node@16/bin/node
  npm: 8.19.2 - /opt/homebrew/opt/node@16/bin/npm
Browsers:
  Chrome: 112.0.5615.49
  Safari: 16.3
npmPackages:
  @storybook/addon-a11y: ^7.0.4 => 7.0.4 
  @storybook/addon-essentials: ^7.0.4 => 7.0.4 
  @storybook/addon-links: ^7.0.4 => 7.0.4 
  @storybook/addon-mdx-gfm: ^7.0.4 => 7.0.4 
  @storybook/manager-api: ^7.0.4 => 7.0.4 
  @storybook/nextjs: ^7.0.4 => 7.0.4 
  @storybook/react: ^7.0.4 => 7.0.4 
  @storybook/theming: ^7.0.4 => 7.0.4

Additional context

No response

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 14
  • Comments: 25 (7 by maintainers)

Most upvoted comments

I’ll try to find a solution in the upcoming weeks! Stay tuned!

Be cautious with this workaround though. Next.js uses a custom compiled and enhanced version of SWC. The swc-loader wouldn’t pick it up and therefore a lot of issues may appear with this solution.

Still, thank you for letting us know!

I added:

{
            test: /\.(ts|tsx?|js|jsx?)$/,
            use: {
              loader: 'babel-loader',
              options: {
                presets: ['next/babel'],
              },
            },
          },

To my rules and that seemed to work for the most part.

I am facing this issue with css exported in internal bundling is not being transpiled when running storybook in nextjs, hence the styling is not applying correctly.

was running storybook with react-vite as well but it is applying since vite did the transpiling automatically. however the webpack5 that storybook/nextjs have still need to be configured manually.

Now I am struggling to configure the webpack5 configuration to handle the internal transpiling. Need help if someone managed to solve this earlier.