Jenkins服务器上排查npm编译依赖包错误问题

背景

升级了Jenkins 2.528.3 LTS,编译一个老项目的前端,Jenkins终端报错,错误定位不难,主要记录安装Jenkins的Linux服务器上使用Node的方法。

报错

  • Error: vite: Rollup failed to resolve import "vue3-json-viewer/dist/index.css" from "src/main.js".
html 复制代码
[36mvite v3.2.7 [32mbuilding for production...[36m[39m
transforming...
[32m✓[39m 4 modules transformed.
[31m[vite]: Rollup failed to resolve import "vue3-json-viewer/dist/index.css" from "src/main.js".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`[39m
[31merror during build:
Error: [vite]: Rollup failed to resolve import "vue3-json-viewer/dist/index.css" from "src/main.js".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
    at onRollupWarning (file:///root/.jenkins/workspace/admin-frontend%E5%89%8D%E7%AB%AF/node_modules/vite/dist/node/chunks/dep-2faf2534.js:45909:19)
    at onwarn (file:///root/.jenkins/workspace/admin-frontend%E5%89%8D%E7%AB%AF/node_modules/vite/dist/node/chunks/dep-2faf2534.js:45680:13)
    at Object.onwarn (file:///root/.jenkins/workspace/admin-frontend%E5%89%8D%E7%AB%AF/node_modules/rollup/dist/es/shared/rollup.js:23263:13)
    at ModuleLoader.handleResolveId (file:///root/.jenkins/workspace/admin-frontend%E5%89%8D%E7%AB%AF/node_modules/rollup/dist/es/shared/rollup.js:22158:26)
    at file:///root/.jenkins/workspace/admin-frontend%E5%89%8D%E7%AB%AF/node_modules/rollup/dist/es/shared/rollup.js:22119:26[39m
Build step 'Execute shell' marked build as failure
SSH: Current build result is [FAILURE], not going to run.
Finished: FAILURE

定位

  • 旧版本(可能是 2.3.x 或更早):
bash 复制代码
dist/
├── index.css
└── index.js
  • 新版本(2.4.1):
bash 复制代码
dist/
├── vue3-json-viewer.css  # 文件名变了!
└── vue3-json-viewer.js

解决

  1. 方式一:修改main.js
  • 替换index.css 为 vue3-json-viewer.css
  1. 方式二:修改package.json
  • 替换"^2.2.2" 为 "2.2.2" 。使用2.2.2 固定版本

命令

Jenkins服务器信息

  • Ubuntu24.04
  • Jenkins 2.528.3 LTS
  • NodeJS16 自动安装

服务器Node命令

  1. 定义当前会话下node和npm环境变量
  • NodeJS16的路径:"~/.jenkins/" 是我安装Jenkins的工作根目录
bash 复制代码
root@ubuntu24:~/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/nodejs16/bin# tree
.
├── corepack -> ../lib/node_modules/corepack/dist/corepack.js
├── node
├── npm -> ../lib/node_modules/npm/bin/npm-cli.js
└── npx -> ../lib/node_modules/npm/bin/npx-cli.js

1 directory, 4 files
  • 添加环境变量
bash 复制代码
export PATH="/root/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/nodejs16/bin:$PATH"
bash 复制代码
root@ubuntu24:~/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/nodejs16/bin# node --version
v16.20.2
root@ubuntu24:~/.jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/nodejs16/bin# npm --version
8.19.4
  • 查看当前安装包信息
bash 复制代码
cd /root/.jenkins/workspace/admin-frontend前端
[root@localhost admin-frontend前端]# npm list vue3-json-viewer
xunku-vue-plus@4.8.1 /root/.jenkins/workspace/admin-frontend前端
└── vue3-json-viewer@2.2.2
  • 使用国内镜像源编译
bash 复制代码
# 清除已下载包和缓存
#rm -rf dist node_modules
#npm cache clean --force

# 设置 npm 淘宝镜像
npm config set registry https://registry.npmmirror.com/
npm config set disturl https://npmmirror.com/dist

# 安装并打包
npm install --legacy-peer-deps \
&& npm run build:prod \
&& cd dist && tar -zcvf dist.tar.gz *
相关推荐
小诗懂技术5 小时前
【网络通信UDP】基础 进程间不同主机的通信
运维·服务器·网络
weixin_440730505 小时前
selenium实战记录-从登陆-首页-进入应用流程用例三种参数化+日志+报告
服务器·selenium·测试工具
Hstar_chen5 小时前
开源一款 HarmonyOS 服务器监控客户端:星辰云巡 1.0.3
服务器·华为·harmonyos
开心大爆炸9 小时前
xrdp 连接 登录对话框输入密码后闪退
linux·运维·服务器
苏生Susheng10 小时前
【软件实施】Linux系统Shell脚本教程
linux·运维·服务器·chrome·spring boot·学习·实施
曹牧10 小时前
C#:文本文件读取
服务器·前端·c#
Ruiery10 小时前
Linux 6.6内核 IOMMU 深度解析(七):DMA API 与 IOMMU 集成 — 从 dma_map_single 到 iommu_map
linux·运维·服务器
snow@li11 小时前
服务器运维:Alibaba Cloud Linux 4 LTS 64位 根目录全景深度解析文章
linux·运维·服务器
苏生Susheng11 小时前
【软件实施】Linux企业运维常用命令手册
java·linux·运维·服务器·springboot·springcloud·软件实施
西索斯coding11 小时前
doubao-seed-2.1-turbo 调用一直 401 怎么办?pro 版同样的 Key 却正常——5 分钟排查定位指南
java·服务器·数据库·ai