Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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' ".

like image 206
Arik Chakma Avatar asked Sep 16 '21 05:09

Arik Chakma


People also ask

How do you use swiper in react JS?

Use inside your file with Swiper and Swiper slide. You should wrap each Swiper slide tag by Swiper tag. You can add a lot of options such as spaceBetween, speed, loop, like that as a default. And also add your imported function like EffectFlip, Navigation, Pagination.

What Swiper react modules do I need to install?

By default Swiper React uses core version of Swiper (without any additional modules). If you want to use Navigation, Pagination and other modules, you have to install them first. Here is the list of additional modules imports: Manipulation - Slides manipulation module (only for Core version)

What is manipulation in Swiper react?

Manipulation - Slides manipulation module (only for Core version) Note, Swiper React component will create required elements for Navigation, Pagination and Scrollbar if you pass these params without specifying its elements (e.g. without navigation.nextEl, pagination.el, etc.) Swiper package contains different sets of CSS, Less and SCSS styles:

What is the useswiper hook in Swiper react?

Swiper React provides useSwiper hook to easliy get the Swiper instance in components inside of Swiper: useSwiperSlide is one more hook for components inside of Swiper slides to get the slide data (same data as in SwiperSlide render function) Swiper React uses "slots" for content distribution.

How to fix Swiper error code 6?

For the first solution, you can downgrade to swiper 6 to fix the error. To downgrade the swiper version first remove the swiper entirely and add version 6.8.4. then import swiper-bundle.min.css and swiper.min.css on the top of your file and it should work. So the updated code will be,


Video Answer


13 Answers

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

npm:

npm install [email protected]

yarn:

yarn add [email protected]

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

like image 176
m-keshavarz Avatar answered Oct 21 '22 04:10

m-keshavarz


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

like image 33
Edwin Tantawi Avatar answered Oct 21 '22 06:10

Edwin Tantawi


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';

like image 12
Shahnad Avatar answered Oct 21 '22 06:10

Shahnad


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 [email protected]

Let me know if this helps :) Happy Hacking

like image 5
Abdullah Azhar Avatar answered Oct 21 '22 06:10

Abdullah Azhar


It's a problem with swiper version7~:

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

use any previous swiper version c:

like image 4
Sergio Villa Avatar answered Oct 21 '22 04:10

Sergio Villa


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';
like image 4
vyvu Avatar answered Oct 21 '22 06:10

vyvu


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";
like image 3
Ferhaan Schmidt Avatar answered Oct 21 '22 05:10

Ferhaan Schmidt


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

import Swiper from 'swiper';

and downgrade swiper to [email protected] version

like image 3
Matinshoon Avatar answered Oct 21 '22 06:10

Matinshoon


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

like image 2
Jan Peeter Avatar answered Oct 21 '22 06:10

Jan Peeter


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';
like image 1
Nachat Ayoub Avatar answered Oct 21 '22 06:10

Nachat Ayoub


// 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

like image 1
shailesh patel Avatar answered Oct 21 '22 05:10

shailesh patel


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.

like image 1
Amirhe Avatar answered Oct 21 '22 04:10

Amirhe


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

npm i swiper@^6.5.1
like image 1
Najathi Avatar answered Oct 21 '22 06:10

Najathi