Error on Services registration -> require(...).default is not a constructor #3862
-
Hello all, I am experiencing an error when registering services in my application - require(...).default is not a constructor I am registering services for IOC like so: this.app.container.singleton('Internal/ReportingService', () => {
return new (require('../app/Services/ReportingService').default)();
}); There are ambient modules for TS and services are imported the following way: import ReportingService from '@ioc:Internal/ReportingService'; Services look like that: export default class ReportingService {
public async someMethod() {}
} I have around 30 services that represent application business logic. Services are using Lucid models. Some of the services bindings are OK but some throw the error. I would appreciate it if someone could point me to where the issue might be. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
You need to use import instead of require no? (there is no default in require) |
Beta Was this translation helpful? Give feedback.
-
Hey @lukfor85 ! 👋🏻 I am not sure what you are trying to do. Here, you are binding internal services for no reason. If your goal is to create a singleton of your service, you can simply export an instance of your class inside your module. class MySingleton {}
const instance = new MySingleton()
export default instance |
Beta Was this translation helpful? Give feedback.
Hey @lukfor85 ! 👋🏻
I am not sure what you are trying to do.
You should use providers only to register third-party packages or part of your application you want to externalize (in a package). The thing you bind should have no relation with your application code (or anything loaded by the IOC).
Here, you are binding internal services for no reason.
If your goal is to create a singleton of your service, you can simply export an instance of your class inside your module.