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.

Storybook example is broken on Next 12.1.5

See original GitHub issue

Verify canary release

  • I verified that the issue exists in Next.js canary release

Provide environment information

Windows 10 - 64bit with npm@latest. The issue probably exists on all environments.

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

next/image changes in 12.1.5 broke static image imports of the Storybook example.

Failed to parse src "static/media/public/nyan-cat.png" on next/image, if using relative image it must start with a leading slash "/" or be an absolute URL (http:// or https://)

I’ve confirmed that the issue exists in 12.1.6-canary.6.

Expected Behavior

Images rendering without an error message.

To Reproduce

npx create-next-app --example with-storybook with-storybook-app npm run storybook Navigate to the Nextjs Images Page tab.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

56reactions
diegohmaccommented, May 4, 2022

Thank you @RyanClementsHax !

This is how it worked for me following Ryan’s approach:

import * as NextImage from "next/image";

const OriginalNextImage = NextImage.default;

Object.defineProperty(NextImage, "default", {
  configurable: true,
  value: (props) => typeof props.src === 'string' ? (
    <OriginalNextImage {...props} unoptimized blurDataURL={props.src} />
  ) : (
    <OriginalNextImage {...props} unoptimized />
  ),
});

Object.defineProperty(NextImage, "__esModule", {
  configurable: true,
  value: true
});
12reactions
RyanClementsHaxcommented, May 1, 2022

After diving deeper into the code I found the root of the issue.

The generated code mentioned above makes the following assignments:

Object.assign(exports.default, exports);
module.exports = exports.default;

For some reason, __esModule: true isn’t preserved on module.exports



console.log('before', module.exports.__esModule) // true

if (typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) {
    Object.assign(exports.default, exports);
    module.exports = exports.default;
}

console.log('after', module.exports.__esModule) // undefined

In order for import Image from 'next/image' to get module.exports.default instead of module.exports, __esModule: true needs to be present on module.exports.

This means that the webpack runtime is treating the export from next/image as if it shouldn’t be treated as an esm, which is what the code that disables image optimization requires:

import * as NextImage from "next/image";

const OriginalNextImage = NextImage.default;

Object.defineProperty(NextImage, "default", {
  configurable: true,
  value: (props) => <OriginalNextImage {...props} unoptimized />,
});

Consequently, because we aren’t disabling image optimization, we are getting a bunch of errors as image optimization doesn’t work outside of a nextjs environment.

Therefore, my question to the maintainers is if __esModule: true should be set, thus meaning that this is a bug, or if this is intended behavior and we should come up with a different solution such as the code below:

import * as NextImage from "next/image";

const OriginalNextImage = NextImage.default;

NextImage.default = props =>
  typeof props.src === 'string' ? (
    <OriginalNextImage {...props} unoptimized blurDataURL={props.src} />
  ) : (
    <OriginalNextImage {...props} unoptimized />
  );

NextImage.__esModule = true;

(The above code feels hacky so if anyone has any better approaches, I’m all ears)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Storybook Addon Next
A no config Storybook addon that makes Next.js features just work in Storybook. ... Supported Features; Requirements; Examples; Getting Started.
Read more >
0 - Stack Overflow
There's an issue with storybook and next 12.1.5, downgrade next to 12.1.4 it should work just fine.
Read more >
5 Tips for integrating Next.js with Storybook - YouTube
Don't get stuck setting up Storybook in Next.js! In this video, Michael Chan (@chantastic) helps you start your Storybook and Next.js ...
Read more >
How to setup Storybook with Next.js and Tailwind CSS
Run yarn storybook to start up the dev server. Then you can start adding some story, here is an example. Story example. //...
Read more >
Blog - Next.js 12.1
We now also have a multi-env Docker example with support for loading different .env files based on the environment. React 18, Server Components, ......
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 Hashnode Post

No results found