Jenkins打包问题

为了方便项目快速打包,通常很多应用我们都会采用jenkins流水线得形式来打包部署,但是经常会发现,npm在jenkins环境下安装依赖会超时或者是安装依赖要花很长时间。这是一个典型的linux环境下npm网络连接超时问题,可以用以下方案设置npm。

1.更换npm镜像源(推荐)

arduino 复制代码
方案A、使用淘宝镜像
npm config set registry https://registry.npmmirror.com/

方案B、使用官方镜像
npm config set registry https://registry.npmjs.org/


方案C.使用临时指定镜像
npm config set registry https://registry.npmjs.org/

2.增加超时时间配置

arduino 复制代码
npm config set timeout 60000
npm config set fetch-timeout 60000
npm config set fetch-retry-mintimeout 20000
npm config set fetch-retry-maxtimeout 120000

3.清理npm缓存

css 复制代码
npm cache clean --force

4.再次安装依赖

css 复制代码
// 使用--legacy-peer-deps参数,去掉依赖之间的强匹配
npm install --legacy-peer-deps

5.检查当前npm配置

arduino 复制代码
npm config list
npm config get registry

6.使用yarn代替npm(如果可以的话)

bash 复制代码
# 安装yarn
npm install -g yarn

# 使用yarn安装依赖
yarn install

7.网络代理配置(如果在代理环境下)

arduino 复制代码
# 设置代理
npm config set proxy http://proxy-server:port
npm config set https-proxy http://proxy-server:port

# 取消代理
npm config delete proxy
npm config delete https-proxy

如果以上操作尝试后,都还不行,那么,为了去掉不必要的安装过程或者解决有些依赖安装不上的问题,我通常是将Windows电脑上的项目中的node_modules整体打包成zip,上传到Jenkins服务器的项目底下,再解压

arduino 复制代码
// 如果是在类linux环境下压缩,用以下命令,如果是在windows环境下压缩,直接右键压缩就行
1、压缩node_modules
zip -r 压缩文件名  要压缩的文件夹或文件

压缩完成后,上传到jenkins服务器,然后在项目底下解压

arduino 复制代码
// 解压
unzip  node_modules.zip

然后执行打包,用 npm run staging 或者 npm run build 看下打包是否成功!

如果打包成功,那就万事大吉了~

但是通常windows下的node_modules如果直接复制到Jenkins(类linux)环境,执行打包时,会报错

csharp 复制代码
vite v4.4.9 building for staging...
✓ 2 modules transformed.
✓ built in 119ms
[esbuild] 
You installed esbuild for another platform than the one you're currently using.
This won't work because esbuild is written with native code and needs to
install a platform-specific binary executable.

Specifically the "@esbuild/win32-x64" package is present but this platform
needs the "@esbuild/linux-x64" package instead. People often get into this
situation by installing esbuild on Windows or macOS and copying "node_modules"
into a Docker image that runs Linux, or by copying "node_modules" between
Windows and WSL environments.

If you are installing with npm, you can try not copying the "node_modules"
directory when you copy the files over, and running "npm ci" or "npm install"
on the destination platform after the copy. Or you could consider using yarn
instead of npm which has built-in support for installing a package on multiple
platforms simultaneously.

以上报错是一个典型的跨平台依赖问题!当从Windows环境复制了 node_modules 到Linux环境,但 esbuild 是一个包含原生二进制文件的包,需要针对特定平台安装。在windows下的 node_modules 中,esbuild 的主包和二进制包@esbuild都是win32-x64的包,而在Linux下需要linux-x64的包。

解决方案:

java 复制代码
1、删除linux环境下node_modules中的esbuild 、@esbuild文件夹
cd node_modules
rm -rf @esbuild esbuild // 删除windows版本的包

安装Linux版本的esbuild包

bash 复制代码
# 安装Linux x64版本的esbuild
npm install @esbuild/linux-x64 esbuild/linux-x64或者npm install @esbuild esbuild

# 或者如果是ARM架构的Linux
npm install @esbuild/linux-arm64 esbuild/linux-arm64
或者
npm install @esbuild esbuild

!!之前我在linux下安装过,所以直接把esbuild相关的包保存下来了,可以直接copy到项目中使用

相关推荐
前端康师傅2 小时前
JavaScript 作用域
前端·javascript
前端缘梦2 小时前
Vue Keep-Alive 组件详解:优化性能与保留组件状态的终极指南
前端·vue.js·面试
我是天龙_绍2 小时前
使用 TypeScript (TS) 结合 JSDoc
前端
云枫晖2 小时前
JS核心知识-事件循环
前端·javascript
Simon_He2 小时前
这次来点狠的:用 Vue 3 把 AI 的“碎片 Markdown”渲染得又快又稳(Monaco 实时更新 + Mermaid 渐进绘图)
前端·vue.js·markdown
eason_fan3 小时前
Git 大小写敏感性问题:一次组件重命名引发的CI构建失败
前端·javascript
无羡仙3 小时前
JavaScript 迭代器
前端
XiaoSong3 小时前
从未有过如此丝滑的React Native开发体验:EAS开发构建完全指南
前端·react.js
掘金者阿豪3 小时前
打通KingbaseES与MyBatis:一篇详尽的Java数据持久化实践指南
前端·后端