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

相关推荐
美狐美颜sdk4 小时前
跨平台直播美颜sdk集成攻略:Android、iOS与Web的统一方案
android·前端·ios
Airser4 小时前
npm启动Taro框架报错
前端·npm·taro
Anlici5 小时前
连载小说大学生课设 需求&架构
前端·javascript·后端
2501_938769996 小时前
React Server Components 进阶:数据预取与缓存
前端·react.js·缓存
xiecoding.cn7 小时前
NPM下载和安装图文教程(附安装包)
npm·node.js·npm install·npm安装·npm是什么·安装npm·npm下载
蒜香拿铁7 小时前
Angular【基础语法】
前端·javascript·angular.js
xiaoxiao无脸男7 小时前
纯css:一个好玩的按钮边框动态动画
前端·css·css3
喜欢你,还有大家7 小时前
集群——GitLab&&Jenkins部署
运维·gitlab·jenkins
rookie_fly7 小时前
基于Vue的数字输入框指令
前端·vue.js·设计模式
元直数字电路验证7 小时前
ASP.NET Core Web APP(MVC)开发中无法全局配置 NuGet 包,该怎么解?
前端·javascript·ui·docker·asp.net·.net