DEV Community

Andy Allison
Andy Allison

Posted on

1

Making react vite and storybook play nice

Another one of those reminder posts. I wanted to add storybook to a react app that I had created with vite. I was getting the following error

ERR! Error [ERR_REQUIRE_ESM]: require() of ES Module /home/bart/src/enra_v3/frontend/.storybook/main.js from /home/bart/src/enra_v3/frontend/node_modules/@storybook/core-common/dist/cjs/utils/interpret-require.js not supported.
ERR! main.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
ERR! Instead rename main.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /home/bart/src/enra_v3/frontend/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).
ERR! 
Enter fullscreen mode Exit fullscreen mode

There is a whole thread of different ways of fixing this but this is the one that helped me most Issues Link

Basically Add a package.json file to the .storybook folder

{
  "type": "commonjs"
}
Enter fullscreen mode Exit fullscreen mode

Boooom!!!!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs