outmoded / discuss

The "mailing list"
99 stars 9 forks source link

AssertionError: Plugin xxx missing dependency yyy #804

Closed nitinsh99 closed 5 years ago

nitinsh99 commented 5 years ago

Hi,

I am using hapi17. I created a simple hapi server and I am trying to register a plugin. In the register method of the plugin, if I supply whole package.json file then server throws an error while starting. For example:

Plugin

const Pkg = require('../package.json');
module.exports = {
    ...Pkg,  <-- If I pass only name and version, then issue is not there
    async register(server, options) {
  }
}

Server

const plugin = require('plugin');
const Hapi = require('@hapi/hapi');

const init = async () => {

    const server = Hapi.server({
        port: 3000,
        host: 'localhost'
    });
    await server.register({ plugin: plugin});
    await server.start();
    console.log('Server running on %ss', server.info.uri);
};

Error AssertionError: Plugin hapi-info missing dependency @hapi/joi

If I pass only name and version in the register method of the plugin, then issue is not there.

Full code with issue: https://drive.google.com/file/d/1HM2ooalDGHexLLt2ieCTCnPc2sCpJYzk/view?usp=sharing

hueniverse commented 5 years ago

Probably because you are passing the package.json of your application, not one created specifically for the plugin... Also, ...Pkg has never been the way to pass package info to the plugin. There is a property for that.

nitinsh99 commented 5 years ago

Probably because you are passing the package.json of your application, not one created

No, please download and check the code I attached, plugin is consuming it's own package.json

Also, ...Pkg has never been the way to pass package info to the plugin. There is a property for that.

That's just spread syntx.. eventually plugins exports something like this

module.exports = {
name : abc,
deps: ...  <----- This is causing the issue... 
devdeps: ....
version : x.x.x,
async register {

 }
}

CC : @hueniverse

Marsup commented 5 years ago

It's failing for the exact reasons @hueniverse already mentioned, because you're using spread and adding way too many unnecessary properties. Read the last paragraph of https://hapi.dev/api/#plugins and use that instead.

nitinsh99 commented 5 years ago

adding way too many unnecessary properties

got it working by using pkg attribute.. not sure if it's worth mentioning in the docs that if using {name, version, async register} syntax NO additional properties should be passed.. also error is very misleading..

When I use spread.. I am passing name, version, deps and additional stuff..

  1. Nowhere it's mentioned in the docs that additional properties should not be passed if using {name, version, async register} syntax
  2. If anything additional is breaking hapi's joi validation. server should at lease tell that that additional is that is causing the validation to fail..
devinivy commented 5 years ago

It makes sense because package.json and plugin attributes both share a dependencies property, but it has different meanings in each context. The solution is to use the pkg attribute as you've done.