[英]Error: Cannot find module 'swiper/react' in next.js
将 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");
在以前的版本 ( 6.8
) 中,我没有看到此错误。 我还检查了迁移指南和 github 问题,但没有找到有用的答案。
问题是 VSCode 中的“导入成本”扩展显示了导入模块的大小,所以我认为 Next.js 或 webpack 导出它存在问题,但不知道如何使其工作。
我从 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>
);
}
将 Next.js 升级到11.1.2
,错误信息变为:
Error: Not supported
我找到了解决方案。 它与 next.js 或 webpack 无关。 ESM 模块支持从Node.js 12.20
开始,我的是12.16
。 升级了我的 Node.js 安装,现在一切正常!
https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.