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

相关推荐
hunterandroid16 分钟前
Paging 3 RemoteMediator 实战:构建离线优先的分页列表
android·前端
Underwood_1719 分钟前
星级评价——了解useState
前端
hunterandroid22 分钟前
[鸿蒙从零到一] ArkUI 组件化实战:构建可复用、可组合的自定义组件
前端·华为·架构
一只公羊25 分钟前
iPhone摄像头 在开发/调试过程中强行停止 App,导致 `AVCaptureSession` 没有被正常释放
前端
浮江雾1 小时前
Flutter第十节-----Flutter布局与组件全解析
android·开发语言·前端·学习·flutter·入门
xcLeigh2 小时前
Doubao-Seed-Evolving大模型接入教程|搭建全品类提示词+AI工具导航网页
前端·人工智能·python·ai·html·ai开发·豆包
巴勒个啦3 小时前
Vue 3.6 Vapor Mode 实战:我把一个 Vue3 项目的渲染性能提升了 4 倍
前端·angular.js
不简说3 小时前
# JS 代码技巧 vol.7 — 20 个浏览器 API 实战,自带 API 能干的事别自己封装
前端·javascript·面试
CodexDave3 小时前
数据库连接池耗尽:排查顺序与三层兜底
服务器·前端·数据库·git·云原生·容器·kubernetes