简体   繁体   English

错误:在 next.js 中找不到模块“swiper/react”

[英]Error: Cannot find module 'swiper/react' in next.js

After upgrading Swiper to Version 7.0.7 , I'm getting this annoying error:将 Swiper 升级到版本7.0.7 ,我收到了这个烦人的错误:

Error: Cannot find module 'swiper/react'
.
.
.
Source

.next\server\pages\items.js (1:0) @ Object.swiper/react
> 1 | module.exports = require("swiper/react");

In the previous Version ( 6.8 ) I didn't see this error.在以前的版本 ( 6.8 ) 中,我没有看到此错误。 I also checked the migration guide and github issues but found no helpful answer.我还检查了迁移指南和 github 问题,但没有找到有用的答案。

The thing is "import cost" extension in VSCode is showing the size of the imported module, so I think there's a problem with Next.js or webpack exporting it, but have no idea how to make it work.问题是 VSCode 中的“导入成本”扩展显示了导入模块的大小,所以我认为 Next.js 或 webpack 导出它存在问题,但不知道如何使其工作。

I copy/pasted the exact code from Swiper docs, but will write it here too if it helps:我从 Swiper 文档中复制/粘贴了确切的代码,但如果有帮助,我也会在这里写:

/* _app.js - imported here because Next doesn't allow global css imports elsewhere */
import 'swiper/scss';
/* slider component which is used inside pages/items.js */
import { Swiper, SwiperSlide } from "swiper/react"; /* 72.4k (gzipped 21.4k) */

export default function CategoriesBar() {
   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>
   );
}

Update更新

after upgrading Next.js to 11.1.2 , the error message changed to:将 Next.js 升级到11.1.2 ,错误信息变为:
Error: Not supported

I found the solution.我找到了解决方案。 It was not related to next.js nor webpack .与 next.js 或 webpack 无关 ESM modules support started from Node.js 12.20 and mine was 12.16 . ESM 模块支持从Node.js 12.20开始,我的是12.16 Upgraded my Node.js installation & everything is working fine now!升级了我的 Node.js 安装,现在一切正常!

https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM