1

I'm trying to use fatify swagger in my fastify app and I was unable to start the application with the fastify swagger. i was getting the error. Please help me in resolving the error. Thanks in advance.

This is the error I'm getting when I'm running the app

fastify_app@1.0.0 start D:\dev\fastify_app
 node app.js

{"level":30,"time":1666146720089,"pid":13484,"hostname":"Dell","msg":"Server listening at http://127.0.0.1:3000"}
{"level":30,"time":1666146720113,"pid":13484,"hostname":"Dell","msg":"Server listening at http://[::1]:3000"}
D:\dev\fastify_app\app.js:15
      fastify.swagger()
              ^

TypeError: fastify.swagger is not a function
    at D:\dev\fastify_app\app.js:15:15
    at D:\dev\fastify_app\node_modules\fastify\lib\server.js:67:11
    at Object.cb (D:\dev\fastify_app\node_modules\fastify\lib\server.js:128:15)
    at Server.wrap (D:\dev\fastify_app\node_modules\fastify\lib\server.js:166:21)
    at Object.onceWrapper (events.js:519:28)
    at Server.emit (events.js:400:28)
    at emitListeningNT (net.js:1365:10)
    at processTicksAndRejections (internal/process/task_queues.js:81:21)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! fastify_app@1.0.0 start: `node app.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the fastify_app@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Dell\AppData\Roaming\npm-cache\_logs\2022-10-19T02_32_00_228Z-debug.log

This is my swagger.js file

const fastify = require("fastify")();
exports.options = fastify.register(require("@fastify/swagger"), {
  routePrefix: "/docs",
  exposeRoute: true,
  swagger: {
    info: {
      title: "Fastify API",
      description:
        "Building a blazing fast REST API with Node.js, MongoDB, Fastify and Swagger",
      version: "1.0.0",
    },
    externalDocs: {
      url: "https://swagger.io",
      description: "Find more info here",
    },
    host: "localhost:3000",
    schemes: ["http"],
    consumes: ["application/json"],
    produces: ["application/json"],
  },
});

This is my app.js code

const fastify = require("fastify")({logger:true});
require("dotenv").config();
const PORT = process.env.PORT;

fastify.register(require("./app/Routes/users.route"));
const swagger = require('./swagger')

fastify.get("/", (req, res) => {
  res.send("Hello world");
});

const start = async () => {
  try {
    await fastify.listen({ port: PORT }, () => {
      fastify.swagger()
      console.log(`server is listening on port ${PORT}`);
    });  
  } catch (error) {
    console.log(error);
    process.exit(1);
  }
};
start();
Vamsi
  • 13
  • 4
  • It could be that you are importing `const swagger = require('./swagger')` and you are not using the variable but trying to run `fastify.swagger()`? – Jorge Vargas Nov 12 '22 at 13:36

0 Answers0