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

相关推荐
李姆斯3 小时前
为啥Agent在coding表现这么好,但是在别的领域就是差的不少?
前端·agent·ai编程
普通网友4 小时前
Springboot中使用Elasticsearch(部署+使用+讲解 最完整)
spring boot·elasticsearch·jenkins
虫小宝4 小时前
优惠券省钱APP查询性能优化:Elasticsearch与Canal实现的多维度商品搜索毫秒级响应方案
elasticsearch·性能优化·jenkins
鱼与宇5 小时前
前端Web(html+css+js+vue3)
前端
雪芽蓝域zzs7 小时前
(六)打包优化 + Nginx 部署完整配置 + 项目收尾
前端·vue.js
研☆香7 小时前
聊一聊前端的常见字 关键字
开发语言·前端·javascript
东风破_7 小时前
JWT 1:从一个登录请求开始,理解 React 项目里的 API 层与 Mock
前端·后端
东风破_7 小时前
JWT 3:为什么 Token 要放进 Authorization?Axios 拦截器到底解决了什么?
前端·后端
东风破_7 小时前
JWT 5:路由守卫是什么?把整个 JWT 登录鉴权流程串起来
前端·后端
东风破_7 小时前
JWT 2:HTTP 是无状态的,为什么登录成功后还要给 Token?
前端·后端