swagger-jsdoc vs apidoc
Generating API Documentation from Code Comments
swagger-jsdocapidoc类似的npm包:

Generating API Documentation from Code Comments

apidoc and swagger-jsdoc are both tools designed to generate API documentation directly from source code comments, reducing the need for separate documentation files. apidoc uses a custom set of tags to produce static HTML pages, focusing on simplicity and human readability. swagger-jsdoc adheres to the OpenAPI Specification (formerly Swagger), generating a JSON or YAML definition file that can be rendered by compatible UI tools. While both aim to keep docs in sync with code, they serve different ends of the API lifecycle — one prioritizes quick static views, and the other enables machine-readable contracts for client generation and testing.

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
swagger-jsdoc1,596,9321,789710 kB455 个月前MIT
apidoc75,2699,644556 kB843 年前MIT

apidoc vs swagger-jsdoc: Documentation Standards and Workflow Compared

Both apidoc and swagger-jsdoc solve the same core problem — keeping API documentation close to the implementation code — but they diverge sharply in standards, output, and ecosystem support. For frontend architects, the choice often depends on whether you need human-readable pages or a machine-readable contract for tooling. Let's compare how they handle common engineering requirements.

📜 Specification Standard: Custom Tags vs OpenAPI

apidoc uses its own custom comment syntax.

  • You write tags like @api, @apiName, and @apiParam.
  • Easy to learn but locked into the apidoc ecosystem.
// apidoc: Custom tags
/**
 * @api {get} /user/:id Get User
 * @apiName GetUser
 * @apiGroup User
 * @apiParam {Number} id User's unique ID.
 */

swagger-jsdoc uses the OpenAPI Specification (via JSDoc).

  • You write YAML or JSON structure inside @swagger blocks.
  • Aligns with industry standards for API contracts.
// swagger-jsdoc: OpenAPI tags
/**
 * @swagger
 * /users/{id}:
 *   get:
 *     summary: Get User
 *     parameters:
 *       - in: path
 *         name: id
 *         schema:
 *           type: integer
 */

📄 Output Format: Static HTML vs JSON Spec

apidoc generates a complete static website.

  • Runs via CLI to produce an doc/ folder with HTML, CSS, and JS.
  • Ready to host immediately without extra UI components.
# apidoc: Generate static site
apidoc -i src/ -o doc/
# Result: ./doc/index.html is ready to serve

swagger-jsdoc generates a JSON or YAML specification file.

  • Requires a separate UI library (like swagger-ui-express) to render.
  • Gives you the raw data for other tools to consume.
// swagger-jsdoc: Generate spec object
const swaggerSpec = swaggerJsdoc(options);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
// Result: JSON spec served at /api-docs/swagger.json

🔄 Integration Workflow: Build Step vs Runtime

apidoc is typically a build-time tool.

  • You run it before deploying your frontend or backend.
  • Docs are decoupled from the running server.
// apidoc: package.json script
{
  "scripts": {
    "build:docs": "apidoc -i src/ -o public/docs/"
  }
}

swagger-jsdoc often runs at runtime or build time.

  • Can be injected as middleware in Express or NestJS.
  • Allows docs to reflect dynamic server state if configured.
// swagger-jsdoc: Express middleware setup
const swaggerUi = require('swagger-ui-express');
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
// Docs served directly by the Node.js application

🛠️ Frontend Tooling: Read-Only vs Client Generation

apidoc is primarily for human reading.

  • No official tools to generate TypeScript clients from the output.
  • Frontend teams must manually maintain API types.
// apidoc: Manual type definition needed
// No automatic generation from apidoc comments
interface User {
  id: number;
  name: string;
}

swagger-jsdoc enables automatic client generation.

  • Tools like openapi-typescript can create types from the JSON spec.
  • Reduces drift between backend API and frontend code.
# swagger-jsdoc: Generate types from spec
npx openapi-typescript http://localhost:3000/api-docs/swagger.json -o src/types/api.ts
# Result: TypeScript interfaces generated automatically

🤝 Similarities: Shared Ground Between apidoc and swagger-jsdoc

While the standards differ, both tools share core philosophies and capabilities. Here are key overlaps:

1. 💬 Comment-Driven Development

  • Both extract documentation from code comments.
  • Keep docs next to the logic they describe.
// Both: Documentation lives in comments
/**
 * Documentation here...
 */
function getUser() { /*...*/ }

2. 🚀 Framework Agnostic

  • Work with Express, Koa, NestJS, or plain Node.js.
  • Do not require a specific web framework to function.
// Both: Can be used with any Node.js server
// No framework lock-in for the documentation generator itself

3. 🧩 Extensible Configuration

  • Both support custom templates or UI tweaks.
  • Allow branding adjustments (logos, colors).
// apidoc: template customization
// swagger-jsdoc: custom CSS in Swagger UI options

4. ✅ Validation Support

  • Both can validate comment syntax during generation.
  • Catch errors before deployment.
// Both: CLI tools return error codes on failure
// Useful for CI/CD pipelines

5. 👥 Active Communities

  • Both have long-standing adoption in the Node.js ecosystem.
  • Plenty of examples and third-party plugins available.
// Both: Widely used in production environments
// Community support available via GitHub issues

📊 Summary: Key Similarities

FeatureShared by apidoc and swagger-jsdoc
Source💬 Code comments
Compatibility🚀 Framework agnostic
Customization🧩 Theming and templates
Validation✅ Syntax checking
Ecosystem👥 Mature communities

🆚 Summary: Key Differences

Featureapidocswagger-jsdoc
Standard📜 Custom tags🌐 OpenAPI Specification
Output📄 Static HTML📋 JSON/YAML Spec
Viewer🖥️ Built-in🔌 Requires separate UI
Client Gen❌ Not supported✅ Supported (via OpenAPI tools)
Workflow⚙️ Build step🔄 Build or Runtime
Frontend Value📖 Human reading🤖 Machine-readable contract

💡 The Big Picture

apidoc is like a quick sketch pad 📝 — great for getting human-readable docs up fast with minimal setup. Ideal for internal tools, microservices without public contracts, or teams that do not need automated client generation.

swagger-jsdoc is like a formal blueprint 🏗️ — perfect for teams that treat API specs as contracts. Shines in environments where frontend types, contract testing, and API gateways rely on the OpenAPI standard.

Final Thought: If your frontend team needs to generate TypeScript types or validate API responses automatically, swagger-jsdoc is the clear choice. If you just need a simple page for humans to read without extra tooling, apidoc gets the job done with less configuration.

如何选择: swagger-jsdoc vs apidoc

  • swagger-jsdoc:

    Choose swagger-jsdoc if your team relies on the OpenAPI ecosystem for tooling, such as automatic TypeScript client generation, contract testing, or integration with API gateways. It is the better fit for production-grade services where machine-readable specs are required for frontend consumption, CI/CD validation, or third-party developer portals.

  • apidoc:

    Choose apidoc if you need a lightweight, zero-config solution for generating static HTML documentation quickly without worrying about the OpenAPI standard. It is ideal for internal tools, prototypes, or projects where human readability is the only goal and there is no need for automated client SDK generation or strict contract validation.

swagger-jsdoc的README

swagger-jsdoc

This library reads your JSDoc-annotated source code and generates an OpenAPI (Swagger) specification.

npm Downloads CI

Getting started

Imagine having API files like these:

/**
 * @openapi
 * /:
 *   get:
 *     description: Welcome to swagger-jsdoc!
 *     responses:
 *       200:
 *         description: Returns a mysterious string.
 */
app.get('/', (req, res) => {
  res.send('Hello World!');
});

The library will take the contents of @openapi (or @swagger) with the following configuration:

const swaggerJsdoc = require('swagger-jsdoc');

const options = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: 'Hello World',
      version: '1.0.0',
    },
  },
  apis: ['./src/routes*.js'], // files containing annotations as above
};

const openapiSpecification = swaggerJsdoc(options);

The resulting openapiSpecification will be a swagger tools-compatible (and validated) specification.

swagger-jsdoc example screenshot

System requirements

  • Node.js 20.x or higher

You are viewing swagger-jsdoc v6 which is published in CommonJS module system.

Installation

npm install swagger-jsdoc --save

Or

yarn add swagger-jsdoc

Supported specifications

  • OpenAPI 3.x
  • Swagger 2
  • AsyncAPI 2.0

Validation of swagger docs

By default swagger-jsdoc tries to parse all docs to it's best capabilities. If you'd like to you can instruct an Error to be thrown instead if validation failed by setting the options flag failOnErrors to true. This is for instance useful if you want to verify that your swagger docs validate using a unit test.

const swaggerJsdoc = require('swagger-jsdoc');

const options = {
  failOnErrors: true, // Whether or not to throw when parsing errors. Defaults to false.
  definition: {
    openapi: '3.0.0',
    info: {
      title: 'Hello World',
      version: '1.0.0',
    },
  },
  apis: ['./src/routes*.js'],
};

const openapiSpecification = swaggerJsdoc(options);

Documentation

Click on the version you are using for further details: