5.3.3. Module Options

In this chapter, you’ll learn about passing options to your module from the Medusa application’s configurations and using them in the module’s resources.

What are Module Options?#

A module can receive options to customize or configure its functionality.

For example, if you’re creating a module that integrates a third-party service, you’ll want to receive the integration credentials in the options rather than adding them directly in your code.


How to Pass Options to a Module?#

To pass options to a module, add an options property to the module’s configuration in medusa-config.ts.

For example:

medusa-config.ts
1module.exports = defineConfig({2  // ...3  modules: [4    {5      resolve: "./src/modules/hello",6      options: {7        capitalize: true,8      },9    },10  ]11})

The options property’s value is an object. You can pass any properties you want.


Access Module Options in Main Service#

The module’s main service receives the module options as a second parameter.

For example:

src/modules/hello/service.ts
1import { MedusaService } from "@medusajs/framework/utils"2import MyCustom from "./models/my-custom"3
4// recommended to define type in another file5type ModuleOptions = {6  capitalize?: boolean7}8
9export default class HelloModuleService extends MedusaService({10  MyCustom,11}){12  protected options_: ModuleOptions13
14  constructor({}, options?: ModuleOptions) {15    super(...arguments)16
17    this.options_ = options || {18      capitalize: false,19    }20  }21
22  // ...23}

Access Module Options in Loader#

The object that a module’s loaders receive as a parameter has an options property holding the module's options.

For example:

src/modules/hello/loaders/hello-world.ts
1import {2  LoaderOptions,3} from "@medusajs/framework/types"4
5// recommended to define type in another file6type ModuleOptions = {7  capitalize?: boolean8}9
10export default async function helloWorldLoader({11  options,12}: LoaderOptions<ModuleOptions>) {13  14  console.log(15    "[HELLO MODULE] Just started the Medusa application!",16    options17  )18}
Was this chapter helpful?
Edit this page