You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swagger Theme版本指定已弃用:Node.js后端部署Ubuntu EC2报错

解决"Explicit version indication is deprecated"报错问题

问题原因

报错来自swagger-themes库,核心原因是你在实例化SwaggerTheme时显式指定了版本号('v3'),该用法已被库的新版本弃用。

修复步骤

1. 移除SwaggerTheme构造函数的版本参数

原代码中显式指定版本的写法已失效,改为无参实例化(库默认支持Swagger UI v3版本,无需手动指定):

// 原代码
const theme = new SwaggerTheme('v3');

// 修改后
const theme = new SwaggerTheme();

2. 调整主题样式获取方式(适配swagger-ui-express需求)

部分新版本的swagger-themes中,getBuffer返回的Buffer类型可能不匹配customCss的字符串要求,建议改用getCss方法直接获取样式字符串:

// 原代码
const options = {
    explorer: true,
    customCss: theme.getBuffer('dark'),
};

// 修改后
const options = {
    explorer: true,
    customCss: theme.getCss('dark'),
};

完整修复后的代码片段

const swaggerUi = require('swagger-ui-express');
const swagger = require('./swagger.json');
const { SwaggerTheme } = require('swagger-themes');

swagger.host = process.env.BACKEND_BASE_URL;
swagger.schemes = process.env.BACKEND_BASE_URL == 'localhost:4000' ? ['http'] : ['https'];

const theme = new SwaggerTheme();

const options = {
    explorer: true,
    customCss: theme.getCss('dark'),
};

app.use('/swagger', swaggerUi.serve, swaggerUi.setup(swagger, options));

const port = config.port || 4000;
const server = app.listen(port, () => {
    log(color.yellow(' ******************************************** '));
    log(color.yellow(' *******                    ******* '));
    log(color.yellow(` ******* Server is running on ${port}    ******* `));
    log(color.yellow(' *******                              ******* '));
    log(color.yellow(' ******************************************** '));
});

验证修复

修改代码后重启服务:

pm2 restart <你的服务名称>

服务启动后访问/swagger路径,即可正常加载带有dark主题的Swagger UI,同时能看到服务运行的端口信息。

内容的提问来源于stack exchange,提问作者hamza malik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 09:32:08