swiper: Could not find a declaration file for module 'swiper'

Check that this is really a bug

  • I confirm

Reproduction link

n/a

Bug description

Fresh installation of Vite with React and TypeScript using Swiper version 9.3.2. I am getting the following error:

Could not find a declaration file for module 'swiper'. 

'/.../node_modules/swiper/swiper.esm.js' implicitly has an 'any' type.

There are types at '/.../node_modules/swiper/swiper.d.ts', but this result could not be resolved when respecting package.json "exports".

The 'swiper' library may need to update its package.json or typings.ts

Here’s my code:

// Swiper modules
import { Keyboard } from "swiper"; // This is the problem

// Swiper React components
import { Swiper, SwiperSlide, SwiperRef, SwiperClass } from "swiper/react"; // This is fine

// Swiper styles
import "swiper/css"; // This is fine

Expected Behavior

Types should just work without error.

Actual Behavior

Type error.

Swiper version

9.3.2

Platform/Target and Browser Versions

n/a

Validations

  • Follow our Code of Conduct
  • Read the docs.
  • Check that there isn’t already an issue that request the same feature to avoid creating a duplicate.
  • Make sure this is a Swiper issue and not a framework-specific issue

Would you like to open a PR for this bug?

  • I’m willing to open a PR

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 16

Most upvoted comments

Solution:

  1. Create a swiper.d.ts file in your application folder
  2. Inside the file, place the following line of code: declare module 'swiper'

It helps for me: “strict”: true -> “strict”: false

in your tsconfig.json

I had the same problem in my React TypeScript project initialized with Vite. I fixed that error by removing "noUncheckedSideEffectImports": true from my tsconfig.app.json file

@paras1729kori You can try this solution: in your tsconfig file add the following code

"compilerOptions": {
"noImplicitAny": false
}

P.S it’s not the best option, but it might come in handy

Got same error today while migrating nextjs project to esm. tsconfig moduleResolution = bundler

Im just copy-pasted declaraions from swiper.d.ts into my project’s global.d.ts

declare module 'swiper' {
  import Swiper from './types/swiper-class'
  import { SwiperOptions } from './types/swiper-options'
  import { SwiperModule } from './types/shared'

  declare const A11y: SwiperModule
  declare const Autoplay: SwiperModule
  declare const Controller: SwiperModule
  declare const EffectCoverflow: SwiperModule
  declare const EffectCube: SwiperModule
  declare const EffectFade: SwiperModule
  declare const EffectFlip: SwiperModule
  declare const EffectCreative: SwiperModule
  declare const EffectCards: SwiperModule
  declare const HashNavigation: SwiperModule
  declare const History: SwiperModule
  declare const Keyboard: SwiperModule
  declare const Lazy: SwiperModule
  declare const Mousewheel: SwiperModule
  declare const Navigation: SwiperModule
  declare const Pagination: SwiperModule
  declare const Parallax: SwiperModule
  declare const Scrollbar: SwiperModule
  declare const Thumbs: SwiperModule
  declare const Virtual: SwiperModule
  declare const Zoom: SwiperModule
  declare const FreeMode: SwiperModule
  declare const Grid: SwiperModule
  declare const Manipulation: SwiperModule

  export default Swiper
  export {
    Swiper,
    SwiperOptions,
    A11y,
    Autoplay,
    Controller,
    EffectCoverflow,
    EffectCube,
    EffectFade,
    EffectFlip,
    EffectCreative,
    EffectCards,
    HashNavigation,
    History,
    Keyboard,
    Lazy,
    Mousewheel,
    Navigation,
    Pagination,
    Parallax,
    Scrollbar,
    Thumbs,
    Virtual,
    Zoom,
    FreeMode,
    Grid,
    Manipulation,
  }
}

Issue is still happening as of today. Basically, the very first step of the Getting Started part of the documentation is broken as doing:

import ‘swiper/css’;

throws this TypeScript error: Cannot find module ‘swiper/css’ or its corresponding type declarations.

I’m still facing the same issue even today after installing swiper.

The fix above worked for me.

Create a swiper.d.ts file in your src folder(I’m using vite + react + swiper v11.2.6) Inside the file, place the following line of code: declare module ‘swiper’

Another solution is to import the css modules from a .css file instead of a JSX/TSX file:

@import "swiper/css/pagination";
@import "swiper/css/scrollbar";
@import "swiper/css";
@import "swiper/css/effect-coverflow";