0

I am getting a syntax error on trying to set up swagger with typescript express app. When I tried this without the swagger-jsdoc i.e using swagger.json it was working but when I am trying to use swagger-jsdoc for documenting I am facing the following error So please can any body help me in resolving this issue.

This is my app.ts

import bodyParser from "body-parser";
import express, { RequestHandler } from "express";
import morgan from "morgan";
import v1 from "./routes/v1";
import swaggerUi from "swagger-ui-express";
import swaggerJsdoc from "swagger-jsdoc";

const app = express();

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

const swaggerDefinition = {
    openapi: "3.0.0",
    info: {
        title: "Homework APP",
        version: "0.1.0",
        description: "This is a simple CRUD API application made with Express and documented with Swagger",
    },
    servers: [
        {
            "url": "/",
            "description": "Local Dev"
        },
    ],
}

const options = {
    swaggerDefinition,
    apis: ["./src/routes/v1/*.ts"],
}

const specs = swaggerJsdoc(options);


app.use(morgan(":status :method :url :response-time ms"));
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(specs))
app.use(v1);

export default app;

ERROR

(function (exports, require, module, __filename, __dirname) { import { prepare, extract, organize, finalize } from './src/specification.js';
                                                                     ^

SyntaxError: Unexpected token {
    at new Script (vm.js:79:7)
    at createScript (vm.js:251:10)
    at Object.runInThisContext (vm.js:303:10)
    at Module._compile (internal/modules/cjs/loader.js:656:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
    at Function.Module._load (internal/modules/cjs/loader.js:529:3)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/home/aman/Documents/Octo/homeworkapp-server/src/app.ts:6:1)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Module.m._compile (/home/aman/Documents/Octo/homeworkapp-server/node_modules/ts-node/src/index.ts:1056:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/home/aman/Documents/Octo/homeworkapp-server/node_modules/ts-node/src/index.ts:1059:12)
    at Module.load (internal/modules/cjs/loader.js:598:32)
[nodemon] app crashed - waiting for file changes before starting...

1 Answer 1

1

I was able to resolve this issue by rolling back the swagger-jsdoc version to "swagger-jsdoc": "^6.0.0"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.