38

I'm also having the same problem with the latest version of Swiper. It worked on my previous project but not working right now. Not even that version. Tried on the latest version too.

Here is my code.

// Import Swiper React components
import { Swiper, SwiperSlide } from "swiper/react";

// Import Swiper styles
import "swiper/css";

const App = () => {
  return (
    <Swiper
      spaceBetween={50}
      slidesPerView={3}
      onSlideChange={() => console.log("slide change")}
      onSwiper={(swiper) => console.log(swiper)}
    >
      <SwiperSlide>Slide 1</SwiperSlide>
      <SwiperSlide>Slide 2</SwiperSlide>
      <SwiperSlide>Slide 3</SwiperSlide>
      <SwiperSlide>Slide 4</SwiperSlide>
      ...
    </Swiper>
  );
};

export default App;

Whenever I run my code it says, " Module not found: Can't resolve 'swiper/react' ".

Arik Chakma
  • 481
  • 1
  • 4
  • 4

14 Answers14

40

swiper version 7 only works if you have pure ESM. if not you have to downgrade to version 6.8.4

npm:

npm install swiper@6.8.4

yarn:

yarn add swiper@6.8.4

then add the structure like below:

import { Swiper, SwiperSlide } from "swiper/react";
import 'swiper/swiper-bundle.min.css'
import 'swiper/swiper.min.css'

<Swiper
      spaceBetween={50}
      slidesPerView={3}
      centeredSlides
      onSlideChange={() => console.log("slide change")}
      onSwiper={swiper => console.log(swiper)}
    >
      <SwiperSlide>Slide 1</SwiperSlide>
      <SwiperSlide>Slide 2</SwiperSlide>
      <SwiperSlide>Slide 3</SwiperSlide>
      <SwiperSlide>Slide 4</SwiperSlide>
    </Swiper>

version 6.8.4 documentation is here

m-keshavarz
  • 667
  • 3
  • 6
32

Create React App doesn't support pure ESM packages yet. It is still possible to use Swiper (7.2.0+) with it.

In this case we need to use direct file imports

// Core modules imports are same as usual
import { Navigation, Pagination } from 'swiper';
// Direct React component imports
import { Swiper, SwiperSlide } from 'swiper/react/swiper-react.js';

// Styles must use direct files imports
import 'swiper/swiper.scss'; // core Swiper
import 'swiper/modules/navigation/navigation.scss'; // Navigation module
import 'swiper/modules/pagination/pagination.scss'; // Pagination module

source: Swiper docs | https://swiperjs.com/react#usage-with-create-react-app

Edwin Tantawi
  • 421
  • 4
  • 4
  • 13
    Why on earth would that be pushed all the way to the bottom of the page without any reference from the top where it specifically tells you how to start using swiper? – Phill Healey Nov 05 '21 at 11:55
  • 6
    If you do a clean install of create-react-app and add this it still doesn't work, gives a `Module not found: Can't resolve 'swiper/modules/navigation/navigation.css' in` error – HardBurn Nov 29 '21 at 15:19
  • Thx, swiper.js 7.4.1 also works – sdym Jan 11 '22 at 08:51
  • 1
    @HardBurn We were able to get around this by patching the package.json file by appending `"./swiper.scss": "./swiper.scss"`, or whatever you need to the exports – Don P Jan 21 '22 at 22:39
13

There is no index file in the swiper module. so just change the import path from

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

 to

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

Shahnad S
  • 983
  • 10
  • 17
6

Fix Swiperjs on React It seems issue with latest version of swiperjs.

  • uninstall swiperjs if you already added to your react project

    npm uninstall swiper

  • install Swiperjs version 6.0.2

    npm install swiper@6.0.2

Let me know if this helps :) Happy Hacking

5

I'm using:

import { Swiper, SwiperSlide } from 'swiper/react/swiper-react';
import 'swiper/swiper.min.css';

Source: github.com/nolimits4web/swiper/issues/4871

UPDATED

Now, if you upgrade react-scripts to 5.0.0, you can use:

import { Swiper, SwiperSlide } from 'swiper/react';
import 'swiper/css';
vyvu
  • 51
  • 1
  • 3
4

It's a problem with swiper version7~:

https://github.com/nolimits4web/swiper/issues/4871

use any previous swiper version c:

Sergio Villa
  • 41
  • 1
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 19 '21 at 20:48
  • Gatsby complier solved my problem. – Arik Chakma Sep 20 '21 at 03:53
3

I had the same problem, fixed with changing the import to:

import Swiper from 'swiper';

and downgrade swiper to swiper@6.0.2 version

Matinshoon
  • 55
  • 7
3

Iam use swiper 6.8.4 you need to use direct file imports like :

import { Swiper, SwiperSlide } from "swiper/react/swiper-react";
import "swiper/swiper-bundle.min.css";
import "swiper/swiper.min.css";
import 'swiper/modules/effect-fade/effect-fade';
import "swiper/modules/navigation/navigation";
import "swiper/modules/pagination/pagination";
2

as @vyvu said if you now upgrade react-scripts to 5.0.0, you can use:

// I am using swiper version "^8.1.0"
import { Swiper, SwiperSlide } from 'swiper/react';
import 'swiper/css';
Nachat Ayoub
  • 359
  • 2
  • 8
2

Changing path to swiper/react/swiper-react.js also worked for me

Jan Peeter
  • 347
  • 3
  • 12
1

Here is what swiper team did in their demos e.g. react-manipulation-example.
They are using Vite for build and development.

  • package.json
  "scripts": {
    "dev": "vite",
    "build": "tsc && vite build",
    "serve": "vite preview"
  },
  • vite.config.ts
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()]
})

Here is how to migration existing CRA to vite
Note: It might need to create a new fresh version of vite project.

Although the activities of some team members to address this issue appear to be exhausted, I believe migrating to the prior version is not the best action plan. Referencing certain gist for migrating to the pure ESM package, as well as some difficulties with create-react-app that are perplexing #10933, #10892, ..., might not have good development experience.

amirhe
  • 2,186
  • 1
  • 13
  • 27
1

please use this version. It's worked for me.

npm i swiper@^6.5.1
Najathi
  • 2,529
  • 24
  • 23
1

// Core modules imports are same as usual
import { Navigation, Pagination } from 'swiper';
// Direct React component imports
import { Swiper, SwiperSlide } from 'swiper/react/swiper-react.js';

// Styles must use direct files imports
import 'swiper/swiper.scss'; // core Swiper
import 'swiper/modules/navigation/navigation.scss'; // Navigation module
import 'swiper/modules/pagination/pagination.scss'; // Pagination module

OR you can check link => https://swiperjs.com/react#usage-with-create-react-app

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32032775) – JBS Jun 21 '22 at 17:55
1

Upgrade to the latest SwiperJS version, for example ^8.4.6

npm:

npm install swiper

yarn:

yarn add swiper

Code sample:

import {Swiper, SwiperSlide} from "swiper/react";
import { Navigation, Pagination, Scrollbar, A11y, EffectCube } from 'swiper';
import 'swiper/swiper-bundle.min.css';

export const Slider = ({ slides }) => {
  return (
    <Swiper
      modules={[Navigation, Pagination, Scrollbar, A11y, EffectCube]}
      spaceBetween={50}
      slidesPerView={3}
      navigation
      pagination={{ clickable: true }}
      scrollbar={{ draggable: true }}
      onSlideChange={() => console.log('slide change')}
      onSwiper={(swiper) => console.log(swiper)}
      effect={"cube"}
      cubeEffect={{
        shadow: true,
        slideShadows: true,
        shadowOffset: 20,
        shadowScale: 0.94,
      }}
    >
      {slides.map((slide) => (
        <SwiperSlide key={slide.image}>
          <img src={slide.image} alt={slide.title}/>
        </SwiperSlide>
      ))}
    </Swiper>
  )

}

Swiper React is available only via NPM as a part of the main Swiper library, but import is the nest:

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

Don't forget to use SwiperSlide component.

In case some additional plugins are required, import from 'swiper', for example

import { Navigation, Pagination, Scrollbar, A11y, EffectCube } from 'swiper';

Working code sample - React Swiper

Explanation reference - How to use react-swiperjs

Maksym Rudnyi
  • 443
  • 3
  • 7