No operations defined in spec! - I get this error even though the swagger is setup and the end points are defined

I am trying to setup swagger on top of my node application using the swagger npm package. I have my end points and swagger setup perfect(atleast almost perfect), I did do quiet a lot of research on whats going wrong but I couldn't find the trace. My swagger setup file:

const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const swaggerJSDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
const abc= require('./routes/abc');
var app = express();

const swaggerDefinition = {
    info: {
        title: 'Analytics Project',
        version: '1.0.0',
        description: 'Analytics API swagger documentation'
    }
};
const options = {
    swaggerDefinition,
    apis: ['./routes/abc.js']
};

const swaggerSpec = swaggerJSDoc(options);

var api = require('./routes/abc');
app.use('/', api);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/api/v1/abc', abc);
app.use('/api/v1/abc/scatter', abc);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
module.exports = app;

My end points are defined in ./routes/abc.js:

var express = require('express');
var router = require('express').Router();
const request = require('request');

/**
 * @swagger
 * /:
 *   get:
 *     description:  Endpoint for everything
 */
router.get('/', function(req, res, next) { //End point1
    res.send('hello from dataservice');
  });

/**
 * @swagger
 * /scatter:
 *   post:
 *    description:  Endpoint for all variations of scatter plots
 */
router.post('/scatter', function(req, res, next) { //end point2
    res.json({users: [{name: 'Timmy'}]});
  });
module.exports = router;

I was expecting the 2 end points to show up on the page. But instead I get the 'No operations defined in spec!' error. What am I missing? Any help is appreciated.


Solution 1:

It may be that there is an issue with how you are referencing your routes. The referencing must always start from the root of your application. So './routes/abc.js' must be changed to 'the-folder-name-in-root/routes/abc.js'

Solution 2:

Try change apis path from apis: ['./routes/abc.js'] to apis: [`${__dirname}/routes/abc.js`] to make it the full path from the root folder. That works for me.