swiper报错The requested module 'react' is expected to be of type CommonJS, which does not su

低版本node与swiper版本导致的错误

最近在nextjs项目中使用swiper,版本信息如下:

node版本12.9.1

"next": "11.1.0",
"swiper": "7.4.1",

项目打包后报错:

(node:159) ExperimentalWarning: The ESM module loader is experimental.

> Build error occurred
file:///shark/web-book/node_modules/swiper/react/use-isomorphic-layout-effect.js:1
import { useEffect, useLayoutEffect } from 'react';
         ^^^^^^^^^
SyntaxError: The requested module 'react' is expected to be of type Commonjs, which does not support named exports. Commonjs modules can be imported by importing the default export.
For example:
import pkg from 'react';
const { useEffect, useLayoutEffect } = pkg;
    at ModuleJob._instantiate (internal/modules/esm/module_job.js:97:21)
    at async ModuleJob.run (internal/modules/esm/module_job.js:143:20)
    at async Loader.import (internal/modules/esm/loader.js:182:24) {
  type: 'SyntaxError'
}

重点错误信息The requested module 'react' is expected to be of type Commonjs, which does not support named exports. Commonjs modules can be imported by importing the default export

意思就是打包环境还不支持EMS包

解决方案:

  • 升级node版本到14.x
  • 把swiper版本降到6.x版本

相关文章

一、前言 在组件方面react和Vue一样的,核心思想玩的就是组件...
前言: 前段时间学习完react后,刚好就接到公司一个react项目...
前言: 最近收到组长通知我们项目组后面新开的项目准备统一技...
react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...