zazoomauro / node-dependency-injection

The NodeDependencyInjection component allows you to standarize and centralize the way objects are constructed in your application.
https://github.com/zazoomauro/node-dependency-injection/wiki
MIT License
281 stars 34 forks source link

Error: TypeError: Bind must be called on a function // container.compile(); #107

Closed Nietzen closed 5 years ago

Nietzen commented 5 years ago

I have the next error:

/node_modules/node-dependency-injection/dist/lib/Compiler.js:45 throw error; ^ TypeError: Bind must be called on a function at bind () at InstanceManager._getNotSyntheticInstanceFromDefinition (/Users/nietzen/projects/ifa/ifa-calendar/backend/node_modules/node-dependency-injection/dist/lib/InstanceManager.js:177:51) at InstanceManager.getInstanceFromDefinition (/Users/nietzen/projects/ifa/ifa-calendar/backend/node_modules/node-dependency-injection/dist/lib/InstanceManager.js:158:21) at OptimizePass.process (/Users/nietzen/projects/ifa/ifa-calendar/backend/node_modules/node-dependency-injection/dist/lib/CompilerPass/OptimizePass.js:39:54) at CompilerPass.process (/Users/nietzen/projects/ifa/ifa-calendar/backend/node_modules/node-dependency-injection/dist/lib/CompilerPass/index.js:130:24) at Compiler._optimize (/Users/nietzen/projects/ifa/ifa-calendar/backend/node_modules/node-dependency-injection/dist/lib/Compiler.js:90:37) at Compiler.run (/Users/nietzen/projects/ifa/ifa-calendar/backend/node_modules/node-dependency-injection/dist/lib/Compiler.js:37:16) at ContainerBuilder.compile (/Users/nietzen/projects/ifa/ifa-calendar/backend/node_modules/node-dependency-injection/dist/lib/ContainerBuilder.js:130:36) at Object. (/Users/nietzen/projects/ifa/ifa-calendar/backend/src/container/Container.js:11:11) at Module._compile (module.js:652:30) I do: const { ContainerBuilder, YamlFileLoader } = require('node-dependency-injection'); const path = require('path'); const container = new ContainerBuilder(true); const loader = new YamlFileLoader(container); loader.load(path.join(__dirname, '..', 'config', 'app.yml')); container.compile(); // error here console.log('definitions', container.definitions); console.log('services', container.services); console.log('mailer', container.getDefinition('app.mailer')); module.exports = container;
zazoomauro commented 5 years ago

@Nietzen Can you please provide me your app.yml file?

Nietzen commented 5 years ago

Hey @zazoomauro sure: app.yml:

services:
    app.mailer:
        class: ../../service/MailService

MailsService.js:

module.exports = class MailService {
    send() {
        console.log('Hola Mundo');
    }
}
zazoomauro commented 5 years ago

@Nietzen the error is in your MailsService.js file

module.exports.default = class MailService {
    send() {
        console.log('Hola Mundo');
    }
}

the difference is module.exports.default instead of module.exports

Nietzen commented 5 years ago

Hey @zazoomauro

I review the code how you load the class from yaml file and notice what you load the class from export.default, i been do a module.exports = class, but the code expected a exports.default = class.

Please fix the documentation.

// good exports.default = class MailService { send() { console.log('hi world!'); }

constructor() {} };

// bad exports = class MailService { send() { console.log('hi world!'); }

constructor() {} };

zazoomauro commented 5 years ago

My documentation says

export default class ...

as ES6 way