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

为Restify(TypeScript)服务集成Swagger文档遇阻求助

Restify + TypeScript 集成Swagger文档踩坑记录

restify-swagger-jsdoc:过时包导致加载演示数据

这个工具是最接近可用状态的,能生成Swagger页面,但存在bug——始终加载演示的petstore数据,原因是包已过时。使用代码如下:

import restify from "restify";
import * as swagger from "restify-swagger-jsdoc"

const server = restify.createServer();
server.use(restify.plugins.bodyParser());

swagger.createSwaggerPage({
    title: 'API documentation',
    version: '1.0.0',
    server: server,
    path: '/docs',
    apis: ['./src/server.ts']
});

swagger-jsdoc & swagger-ui:模块语法冲突循环报错

使用以下代码后出现语法错误:

import restify from "restify";
import swaggerJSDoc from "swagger-jsdoc";
import SwaggerUI from "swagger-ui";

const server = restify.createServer();
server.use(restify.plugins.bodyParser());

const swaggerSpec = swaggerJSDoc({
    definition: {
        openapi: '3.1.0',
        info: {
            title: 'API Documentation',
            version: '1.0.0'
        }
    },
    apis: ['./src/server.ts']
})
const swaggerUi = SwaggerUI({
    spec: swaggerSpec,
    dom_id: '#swagger'
})

报错信息:

project_name\node_modules\react-syntax-highlighter\dist\esm\light.js:1
import highlight from './highlight';
^^^^
语法错误:不能在模块外部使用import语句

按提示在package.json中设置type: "module"后,触发新错误:

Object.defineProperty(exports, "__esModule", { value: true });
引用错误:ES模块作用域中未定义exports

改回type: "commonjs"则回到最初的错误,陷入循环。

swagger-jsdoc & swagger-ui-dist:浏览器环境依赖报错

使用以下代码:

import restify from "restify";
import swaggerJSDoc from "swagger-jsdoc";
import {absolutePath as pathToSwaggerUi, SwaggerUIBundle} from "swagger-ui-dist";

const server = restify.createServer();
server.use(restify.plugins.bodyParser());

const swaggerSpec = swaggerJSDoc({
    definition: {
        openapi: '3.1.0',
        info: {
            title: 'API Documentation',
            version: '1.0.0'
        }
    },
    apis: ['./src/server.ts']
})
const ui = SwaggerUIBundle({
    spec: swaggerSpec,
    dom_id: '#swagger',
    presets: [
        SwaggerUIBundle.presets.apis,
        SwaggerUIBundle.SwaggerUIStandalonePreset
    ],
    layout: "StandaloneLayout"
})

启动服务器时控制台输出swagger-ui-bundle.js的完整内容,同时报错:

引用错误:window未定义


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 08:03:33