fastify-cli icon indicating copy to clipboard operation
fastify-cli copied to clipboard

plugin must be a function or a promise error

Open Adityapanther opened this issue 5 years ago • 6 comments

🐛 Bug Report

After installing fastify-cli and running with command npm start getting error which is written below. if i am running server with normal command node server.js my server running well

Getting error

[email protected] start C:\Users\Aditya_kumar\Desktop\demo_classes\fastify
> fastify start server.js

Error: plugin must be a function or a promise
    at assertPlugin (C:\Users\Aditya_kumar\Desktop\demo_classes\fastify\node_modules\avvio\boot.js:200:11)
    at Boot._addPlugin (C:\Users\Aditya_kumar\Desktop\demo_classes\fastify\node_modules\avvio\boot.js:233:12)
    at Boot.use (C:\Users\Aditya_kumar\Desktop\demo_classes\fastify\node_modules\avvio\boot.js:209:25)
    at Object.server.<computed> [as register] (C:\Users\Aditya_kumar\Desktop\demo_classes\fastify\node_modules\avvio\boot.js:33:14)
    at runFastify (C:\Users\Aditya_kumar\Desktop\demo_classes\fastify\node_modules\fastify-cli\start.js:128:17)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `fastify start server.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] 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\Aditya_kumar\AppData\Roaming\npm-cache\_logs\2021-03-04T04_52_44_630Z-debug.log

Expected behavior

A clear and concise description of what you expected to happen.

// server is running

Your Environment

  • node version: 14, 16, 0
  • fastify version: >=3.13.0
  • fastify version: >=2.8.0
  • os: Windows

Adityapanther avatar Mar 04 '21 05:03 Adityapanther

What is the content of server.js?

mcollina avatar Mar 04 '21 08:03 mcollina

content of server.js file

const fastify = require('fastify');
const app = fastify({logger: true});

// register routes
const rou = require("./routes/firstRoutes");

app.register(rou);

const start = async function(){
    try{
       await app.listen("3000")
    }catch(e){
        app.log.error(e);
    }
}

start();

and content of routes/firstRoutes.js

async function hell(app, options) {
    
    app.get('/', async (req, res) => {
        return {"greeting":"hello"};
    });

    app.get("/user", async (req, rep) =>{
        return {"user":"he all she"};
    });
};

module.exports = hell;

Adityapanther avatar Mar 04 '21 09:03 Adityapanther

If you read the docs, https://github.com/fastify/fastify-cli#start shows that you need to point it to a plugin to start, not a server. You can start the server just by using node server.

mcollina avatar Mar 05 '21 08:03 mcollina

so, fastify start filename.js for only plugins start

Adityapanther avatar Mar 06 '21 10:03 Adityapanther

yes!

mcollina avatar Mar 06 '21 13:03 mcollina

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Oct 01 '26 03:10 github-actions[bot]