Jsdoc Open ApiTroubleshooting

Troubleshooting

Last updated:

Troubleshooting

Common issues and solutions when working with jsdoc-open-api.

CLI Not Found

Problem: jsdoc-open-api: command not found

Solutions:

  1. Use npx:
npx jsdoc-open-api generate src/
  1. Install dependencies:
npm install commander cli-progress
  1. Check optional dependencies installed:
npm list commander cli-progress

No Paths Generated

Problem: Generated spec has empty paths object

Causes:

  1. JSDoc comments missing @openapi tag:
// ❌ Won't be parsed
/**
 * Get all users
 */
export function getUsers() {}

// ✅ Will be parsed
/**
 * @openapi
 * /users:
 *   get:
 *     summary: Get all users
 */
export function getUsers() {}
  1. Wrong file paths in sources:
// Check your glob patterns
{
    sources: [
        "./src/routes/**/*.js", // Correct path?
    ];
}
  1. Files not matching glob pattern:
# Test your glob patterns
npx glob "./src/routes/**/*.js"

Invalid OpenAPI Specification

Problem: Generated spec fails validation

Solution: Validate required fields:

{
  definition: {
    openapi: "3.0.0",  // Required
    info: {
      title: "My API",   // Required
      version: "1.0.0",  // Required
    },
  },
}

Webpack Plugin Not Working

Problem: Plugin doesn't generate spec in webpack build

Solutions:

  1. Verify webpack dependency:
npm install webpack
  1. Check plugin configuration:
const { SwaggerCompilerPlugin } = require("@visulima/jsdoc-open-api");
const path = require("path");

new SwaggerCompilerPlugin(
    path.join(__dirname, "public/swagger.json"), // Absolute path
    [path.join(__dirname, "src/**/*.js")], // Absolute paths
    {
        /* definition */
    },
    { verbose: true }, // Enable logging
);
  1. Server-side only in Next.js:
webpack: (config, { isServer }) => {
    if (!isServer) return config; // Skip on client

    config.plugins.push(new SwaggerCompilerPlugin(/* ... */));
    return config;
};

TypeScript Errors

Problem: TypeScript type errors with imports

Solution: Install type definitions:

npm install --save-dev @types/swagger-jsdoc openapi-types

Duplicate Paths

Problem: Same path defined multiple times causes conflicts

Solution: Consolidate path definitions:

// ❌ Don't do this
/**
 * @openapi
 * /users:
 *   get:
 *     summary: Get users
 */

/**
 * @openapi
 * /users:
 *   post:
 *     summary: Create user
 */

// ✅ Do this
/**
 * @openapi
 * /users:
 *   get:
 *     summary: Get users
 *   post:
 *     summary: Create user
 */

Schema References Not Working

Problem: $ref references not resolving

Cause: Schema not defined in components

Solution: Define schemas first:

/**
 * @openapi
 * components:
 *   schemas:
 *     User:
 *       type: object
 *       properties:
 *         id:
 *           type: integer
 */

/**
 * @openapi
 * /users:
 *   get:
 *     responses:
 *       200:
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/User'
 */

Performance Issues

Problem: Generation is slow

Solutions:

  1. Limit source files:
{
    sources: [
        "./src/routes/**/*.js",
        "!./src/**/*.test.js", // Exclude tests
        "!./node_modules/**", // Exclude node_modules
    ];
}
  1. Use specific paths:
// ❌ Slow
sources: ["./src/**/*.js"];

// ✅ Faster
sources: ["./src/routes/**/*.js", "./src/controllers/**/*.js"];
  1. Cache results:
let cachedSpec = null;

async function getSpec() {
    if (!cachedSpec) {
        cachedSpec = await jsdocOpenApi({
            /* options */
        });
    }
    return cachedSpec;
}

YAML Syntax Errors

Problem: YAML syntax errors in JSDoc comments

Common mistakes:

// ❌ Wrong indentation
/**
 * @openapi
 * /users:
 * get:
 *   summary: Get users
 */

// ✅ Correct indentation (2 spaces)
/**
 * @openapi
 * /users:
 *   get:
 *     summary: Get users
 */

// ❌ Missing colon
/**
 * @openapi
 * /users
 *   get:
 *     summary: Get users
 */

// ✅ Colon after path
/**
 * @openapi
 * /users:
 *   get:
 *     summary: Get users
 */

Debugging

Enable verbose logging:

# CLI
npx jsdoc-open-api generate -v src/

# Very verbose (debugging)
npx jsdoc-open-api generate -d src/

Programmatic:

const spec = await jsdocOpenApi({
    definition: {
        /* ... */
    },
    sources: ["./src/**/*.js"],
    verbose: true, // Enable logging
});

Validation

Validate your generated spec:

# Using Swagger CLI
npm install -g @apidevtools/swagger-cli
swagger-cli validate swagger.json

# Using online validator
# Upload to https://editor.swagger.io/
Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues