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
Solution:
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": truefrom mytsconfig.app.jsonfile@paras1729kori You can try this solution: in your
tsconfigfile add the following codeP.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.tsinto my project’sglobal.d.tsIssue 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:
cc https://github.com/nolimits4web/swiper/discussions/6103