I have this component where I am importing the required and modules from Swiper JS to the component. For some reason the files are not imported or working at all. This only happens when using Next 13. Is there something like a trick or workaround?
"use client";
import { Swiper, SwiperSlide } from "swiper/react";
// Import Swiper styles
import "swiper/css";
import "swiper/css/pagination";
// import required modules
import { Pagination } from "swiper";
export default function Home() {
return (
<Swiper
pagination={{
dynamicBullets: true,
}}
modules={[Pagination]}
className="mySwiper"
>
<SwiperSlide>Slide 1</SwiperSlide>
<SwiperSlide>Slide 2</SwiperSlide>
<SwiperSlide>Slide 3</SwiperSlide>
<SwiperSlide>Slide 4</SwiperSlide>
<SwiperSlide>Slide 5</SwiperSlide>
<SwiperSlide>Slide 6</SwiperSlide>
<SwiperSlide>Slide 7</SwiperSlide>
<SwiperSlide>Slide 8</SwiperSlide>
<SwiperSlide>Slide 9</SwiperSlide>
</Swiper>
);
}
Package.json:
{
"name": "swiper-app",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@types/node": "18.15.11",
"@types/react": "18.0.35",
"@types/react-dom": "18.0.11",
"autoprefixer": "10.4.14",
"eslint": "8.38.0",
"eslint-config-next": "13.3.0",
"next": "13.2.4",
"postcss": "8.4.21",
"react": "18.2.0",
"react-dom": "18.2.0",
"swiper": "9.1.1",
"tailwindcss": "3.3.1",
"typescript": "5.0.4"
}
}
2
Answers
Method 1:
Try using CDN instead.
Not using the experimental
/app
directory:Create a file called
_document.js
inside thepages
folder. And then add swiperjs css CDN.Using The experimental
/app
directory:Edit the
app/layout.tsx
file (Note: it can be layout.js, layout.jsx, layout.ts, layout.tsx depending on your project)Method 2:
Try enabling
es-modules-support
in your Next.js project.To do this:
Edit the
next.config.js
file. And put this:Try importing "swiper/swiper.min.css" instead of "swiper/css"; this works fine with the app structure.