惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
罗磊的独立博客
H
Help Net Security
I
Intezer
G
Google Developers Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
Troy Hunt's Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
U
Unit 42
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
News and Events Feed by Topic
J
Java Code Geeks
S
Security Affairs
T
The Blog of Author Tim Ferriss
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
D
Docker
The GitHub Blog
The GitHub Blog
F
Full Disclosure
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
S
Security @ Cisco Blogs
腾讯CDC
人人都是产品经理
人人都是产品经理
M
MIT News - Artificial intelligence
Blog — PlanetScale
Blog — PlanetScale
T
Threatpost
D
DataBreaches.Net
Recent Announcements
Recent Announcements
博客园 - 三生石上(FineUI控件)
MongoDB | Blog
MongoDB | Blog
博客园 - 【当耐特】
L
LINUX DO - 最新话题
Google Online Security Blog
Google Online Security Blog
S
Schneier on Security
S
Securelist
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Help Net Security
Help Net Security
P
Proofpoint News Feed
Project Zero
Project Zero
S
SegmentFault 最新的问题
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MyScale Blog
MyScale Blog
Google DeepMind News
Google DeepMind News
宝玉的分享
宝玉的分享
Y
Y Combinator Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
博客园 - 叶小钗

博客园 - 冯叶青

OpenAI vs Anthropic API 对比:响应体、请求体、消息格式与工具调用 GitHub Actions 自动部署流程 Git分支自动合并脚本:基于时间戳的冲突解决方案 Next.js lingui.js 多语言自动提取翻译键 - ats-node React实现短信验证码输入组件 Next.js 中优雅地使用 Lottie 动画 Next.js 路由参数更新最佳实践:从 replaceState 到 nextReplaceState 解决 Jenkins 环境下 Lingui 构建报错 "btoa is not defined" git 提交 实现大图自动压缩功能 React lingui.js 多语言自动提取翻译键 - ast node html2canvas 解决截图空白问题 react 实现前端发版监测 JS根据文件名获取文件类型 JS获取本机IP地址 适用于react、vue菜单格式化工具函数 git 内容提交 实现大图拦截功能 JS实现视频截图 next.js 利用中间件(middleware.ts)实现PC与移动路由无缝切换 Android生成签名文件及对apk进行签名 JS-SDK 配置,实现微信分享功能
Jenkins构建时SWR模块导入报错解决方案
冯叶青 · 2025-02-20 · via 博客园 - 冯叶青

问题描述

在本地环境打包正常,但在Jenkins环境构建时遇到了与SWR库相关的模块导入错误。错误信息显示无法从非ECMAScript模块中导入命名导出,只能使用默认导出。主要报错如下:

Can't import the named export 'createContext' from non EcmaScript module (only default export is available)
Can't import the named export 'createElement' from non EcmaScript module (only default export is available)
Can't import the named export 'useCallback' from non EcmaScript module (only default export is available)
// ... 更多类似错误

问题原因

这个问题主要是由于:

  • SWR 2.x版本使用了纯ESM模块规范
  • 项目的webpack配置与新版本SWR的ESM模块系统不兼容
  • Jenkins环境与本地开发环境的构建配置差异

解决方案

1. 降级SWR版本

将SWR版本降级到1.3.0版本,这个版本使用了更兼容的模块系统。在package.json中添加:

{
  "dependencies": {
    "swr": "1.3.0"
  }
}

2. 添加版本锁定

为了确保所有依赖包使用相同版本的SWR,在package.json中添加resolutions配置:

{
  "resolutions": {
    "swr": "1.3.0"
  }
}

3. 重新安装依赖

完成配置修改后,需要:

  • 删除node_modules文件夹
  • 删除package-lock.json或yarn.lock文件
  • 重新运行npm install或yarn install
  • 重新构建项目

其他注意事项

如果问题仍然存在,可以检查:1. Jenkins环境中的Node.js版本是否与本地开发环境一致

  • 确保Jenkins使用的npm/yarn版本与本地开发环境一致
  • 如果使用yarn,可以在Jenkins构建脚本中使用yarn install --force

总结

这个问题主要是由于模块系统的兼容性导致的,通过降级SWR版本并确保依赖版本一致性可以解决。这个案例也提醒我们在使用新版本库时要注意模块系统的兼容性问题,特别是在不同环境下的构建过程中。