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到项目中使用

相关推荐
一叶飘零晋几秒前
【(一)Electron 使用之如何用vite+vue3搭建初始框架】
前端·javascript·electron
光影少年11 分钟前
前端SSR和ssg区别
前端·vue.js·人工智能·学习·react.js
广州华水科技16 分钟前
北斗形变监测传感器在水库安全监测中的应用与发展
前端
凯瑟琳.奥古斯特1 小时前
Bootstrap快速上手指南
开发语言·前端·css·bootstrap·html
精益数智工坊1 小时前
拆解制造业仓库物料管理流程:如何通过标准化仓库物料管理流程解决账实不符难题
大数据·前端·数据库·人工智能·精益工程
恶猫1 小时前
网页自动化模拟操作时,模拟真实按键触发事件【终级方案】
前端·javascript·自动化·vue·网页模拟
小羊Yveesss1 小时前
2026年前端开发新趋势:智能协同、工具革新与场景深耕
前端·ai
Dxy12393102161 小时前
HTML中的Canvas可以干哪些事情
前端·html
悟乙己1 小时前
解析 Agent 时代的 HTML PPT SKILLS: html-ppt-skill
前端·html·powerpoint
ZC跨境爬虫1 小时前
跟着 MDN 学 HTML day_2:(表单分组与高级输入控件实战)
前端·javascript·css·ui·html