4.2.7. 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.js.

For example:

1import { HELLO_MODULE } from "./src/modules/hello"2// ...3
4module.exports = defineConfig({5  // ...6  modules: {7    [HELLO_MODULE]: {8      resolve: "./modules/hello",9      options: {10        capitalize: true,11      },12    },13  },14})

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:

1import { MedusaService } from "@medusajs/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:

1import {2  LoaderOptions,3} from "@medusajs/modules-sdk"4
5// recommended to define type in another file6type ModuleOptions = {7  capitalize?: boolean8}9
10export default 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?