Skip to main content

Modules

Modules are essentially named singletons structured in a dependency graph.

They may be classes (which will be instantiated on-demand):

class MyModule {}
moduleManager.register(MyModule, options);

These classes should have a no-argument constructor.

Or, they can be pre-created instances if they correspond to an existing value, or if their construction is complex:

const myComplicatedModule = new MyComplicatedModule('xyzzy', 'plugh');
moduleManager.registerInstance(myComplicatedModule, options);

You can also register fairly arbitrary objects as instances, but you will need to specify a module name via the registration options:

const foo = {bar: true}
moduleManager.registerInstance(foo, {name: "foo"})

Module Settings

Each module has optional settings (documented in other sections) that control how it is managed.

settingtypepurpose
namestringmodule name; inferred when possible
ModuleClassModuleConstructableclass reference with no-argument constructor
instanceanyresolved singleton instance associated with the module
schemaSchema|objectconfigurable schema definition
injectbooleanwhether to inject dependencies; inferred true if init is not implemented
reloadbooleantrue if the module supports hot reloading of configuration
lifecyclebooleanwhether to run lifecycle methods
isMainbooleanmarks entry point; inferred true if main is implemented
definitionsarrayother module defintions to register
providesstringabstract/interface type name to provide
initConfigstringif full, init receives entire application config
lifecycleInitstringmethod name override; defaults to init
lifecycleStartstring...defaults to start
lifecycleMainstring...defaults to main
lifecycleStopstring...defaults to stop
lifecycleTerminatestring...defaults to terminate

These settings can be provided in one of three ways:

  1. as an individual static value in the module class prepended with module, in camelCase:
class MyModule {
static moduleLifecycle = false;
}
  1. as a member of a static moduleInfo object:
class MyModule {
static moduleInfo = {
lifecycle: false
}
}
  1. as part of the options object passed to the register call:
class MyModule {}
moduleManager.register(MyModule, { lifecycle: false })
tip

Module settings are individually merged with base class settings, allowing selective overrides. Any settings passed to the register call then override any internally defined settings.