Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storybook Can't Import Swiper Module

I am trying to import Swiper for react in to my application and use it in Storybook.

I have the following import:

import { Swiper, SwiperSlide } from 'swiper/react';

This works in the React app but not in Storybook, I get the following error:

ModuleNotFoundError: Module not found: Error: Can't resolve 'swiper/react'

Other React modules have import without a problem. Do I need to change some configuration in Storybook?

like image 797
Michael Edwards Avatar asked Sep 19 '25 16:09

Michael Edwards


1 Answers

Swiper v7+ uses ESM module. To make it work with storybook, you have to update storybook to use webpack5 instead of the default webpack4. Also ensure that your current node version must be equal or higher than 12.20, 14.13 or 16. These are versions that support ESM module.

In my case, I also delete addons @storybook/react line in .storybook/main.js as it still uses webpack4 and raises configuration Validation Error. After that it runs fine.

Full instruction

  • https://gist.github.com/shilman/8856ea1786dcd247139b47b270912324#upgrade

  • https://storybook.js.org/blog/storybook-for-webpack-5/

like image 83
Be Munin Avatar answered Sep 21 '25 07:09

Be Munin