Search code examples
javascriptnode.jshapi.jshapi-swagger

hapijs - Cannot start server before plugins finished registration


So I have Hapi (v17.5.1) and when I have my plugins array as

[
 {
  plugin: good,
  options: {
    reporters: {
      errorReporter: [
        {
          module: 'good-squeeze',
          name: 'Squeeze',
          args: [{ error: '*' }],
        }, {
          module: 'good-console',
        },
        'stderr',
      ],
      infoReporter: [
        {
          module: 'good-squeeze',
          name: 'Squeeze',
          args: [{ log: '*', response: '*' }],
        }, {
          module: 'good-console',
        },
        'stdout',
      ],
    },
  }
]

Let's save it in a variable goodPlugin for the next example.

That is, only with the good plugin and it works fine but when I go and try to add Inert, Vision or Hapi-Swagger, it breaks giving the error Cannot start server before plugins finished registration.

An example:

const HapiSwagger = require('hapi-swagger');
const Inert = require('inert');
const Vision = require('vision');
const Pack = require('../package');
module.exports = [
    Inert,
    Vision,
    // goodPlugin,
    {
        plugin: HapiSwagger,
        options: {
            info: {
              title: Pack.description,
              version: Pack.version,
            },
          },
        }
    ];

Where am I going wrong? I even tried to add this only when the development mode is on, but it gave me the same error.


Solution

  • Do you use await when registering plugins? As suggested per documentation, the plugin registration part should look like this:

    const init = async () => {
    
        await server.register({
            plugin: require('hapi-pino')
        });
    
        await server.start();
        console.log(`Server running at: ${server.info.uri}`);
    };
    
    init();