awilix: Bug: Not beign able to resolve dependency properly

I am trying to resolve a dependency but when I do it the normal way, I get an error.

Container.resolve('AccountIndexValidator');

Outputs:

TypeError: Cannot read property 'AccountIndexValidator' of null
    at Object.resolve (P:\Coobo\Github\spendfy-microservices\account\node_modules\awilix\lib\container.js:207:51)
    at RouteRegister (P:\Coobo\Github\spendfy-microservices\account\src\routes.js:14:19)
    at Object.resolve (P:\Coobo\Github\spendfy-microservices\account\node_modules\awilix\lib\resolvers.js:294:20)
    at Object.resolve (P:\Coobo\Github\spendfy-microservices\account\node_modules\awilix\lib\container.js:250:45)
    at Object.<anonymous> (P:\Coobo\Github\spendfy-microservices\account\src\container\index.js:15:16)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Module._compile (P:\Coobo\Github\spendfy-microservices\account\node_modules\pirates\lib\index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.newLoader [as .js] (P:\Coobo\Github\spendfy-microservices\account\node_modules\pirates\lib\index.js:104:7)  
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (P:\Coobo\Github\spendfy-microservices\account\src\setup.js:1:127)
    at Module._compile (internal/modules/cjs/loader.js:778:30)

But if I access it through the registrations property, it works

Container.registrations.AccountIndexValidator.resolve(Container);

Outputs no Error

Am I missing something or is this a bug?

Node Version: 10.16.3
Awilix Version: 4.2.2

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 16 (9 by maintainers)

Most upvoted comments

You seem to be calling register inside of a constructor or factory function which is messing with Awilix’ internal resolution stack tracker. What you are doing is not supported, and I would strongly recommend you rethink your architecture.

Thank you @JamieCorkhill for spending pretty much your whole day on this!

Sorry, but I don’t want to add support for this because this is a mis-use of the package. Again, I would suggest you take a look at the boilerplate I linked above. 😃

You’re welcome @jeffijoe and I was happy to help. It wasn’t a bad way to spend the day.

Good luck with your architecture @VIMGAS. Take a look at routes, Server, LoggerMiddleware, and LoggerClass as @jeffijoe suggested. You might find that your issue exists there.

Thanks. I’m seeing your error now. I’ll get back to you.

Ok @JamieCorkhill I have reduced the code amount to the minimum in order for the error repository to comply with the @coobo/base package and have already pushed it to the repo.

Is it better now? While you don’t respond, I will keep trying to reproduce the error outside of the @coobo/base package scope.

Also have added a .env file in order not to throw any errors

You are registering literally everything in the container, like asFunction and asClass. This is not intended usage, I think you are overcomplicating things.