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 *
相关推荐
怀旧诚子15 小时前
timeshift之Fedora43设置,已在VM虚拟机验证,待真机验证。
java·服务器·数据库
sdm07042717 小时前
yum和开发工具vim/gcc
linux·服务器·centos
zhaoyufei13317 小时前
RK3568-11.0 设置WiFi p2p静态IP
服务器·tcp/ip·p2p
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ1 天前
Linux 查询某进程文件所在路径 命令
linux·运维·服务器
05大叔1 天前
网络基础知识 域名,JSON格式,AI基础
运维·服务器·网络
安当加密1 天前
无需改 PAM!轻量级 RADIUS + ASP身份认证系统 实现 Linux 登录双因子认证
linux·运维·服务器
woho7788991 天前
不同网段IP的网络打印机,打印、扫描设置
运维·服务器·网络
耗子会飞1 天前
小白学习固定VM虚拟机的centos服务器的IP
运维·服务器·centos
阿达_优阅达1 天前
告别手工对账:xSuite 如何帮助 SAP 企业实现财务全流程自动化?
服务器·数据库·人工智能·自动化·sap·企业数字化转型·xsuite
IMPYLH1 天前
Linux 的 chroot 命令
linux·运维·服务器