4.2.1. Module's Container

In this chapter, you'll learn about the module's container and how to resolve resources in that container.

Module's Container#

Each module has a local container only used by the resources of that module.

So, resources in the module, such as services or loaders, can only resolve other resources registered in the module's container, such as:

  • logger: A utility to log message in the Medusa application's logs.

Resolve Resources#

Services#

A service's constructor accepts as a first parameter an object used to resolve resources registered in the module's container.

For example:

1import { Logger } from "@medusajs/medusa"2
3type InjectedDependencies = {4  logger: Logger5}6
7export default class HelloModuleService {8  protected logger_: Logger9
10  constructor({ logger }: InjectedDependencies) {11    this.logger_ = logger12
13    this.logger_.info("[HelloModuleService]: Hello World!")14  }15
16  // ...17}

Loader#

A loader function accepts as a parameter an object having the property container. Its value is the module's container used to resolve resources.

For example:

1import {2  LoaderOptions,3} from "@medusajs/modules-sdk"4import { Logger } from "@medusajs/medusa"5
6export default function helloWorldLoader({7  container,8}: LoaderOptions) {9  const logger: Logger = container.resolve("logger")10
11  logger.info("[helloWorldLoader]: Hello, World!")12}
Was this chapter helpful?