1 export const metadata = {
2 title: `${pageNumber} Module Options`,
3 }
4
5 # {metadata.title}
6
7 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.
8
9 ## What are Module Options?
10
11 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.
12
13 ---
14
15 ## How to Pass Options to a Module?
16
17 To pass options to a module, add an `options` property to the module’s configuration in `medusa-config.ts`.
18
19 For example:
20
21 ```ts title="medusa-config.ts"
22 module.exports = defineConfig({
23 // ...
24 modules: [
25 {
26 resolve: "./src/modules/blog",
27 options: {
28 capitalize: true,
29 },
30 },
31 ],
32 })
33 ```
34
35 The `options` property’s value is an object. You can pass any properties you want.
36
37 ### Pass Options to a Module in a Plugin
38
39 If your module is part of a plugin, you can pass options to the module in the plugin’s configuration.
40
41 For example:
42
43 ```ts title="medusa-config.ts"
44 import { defineConfig } from "@medusajs/framework/utils"
45 module.exports = defineConfig({
46 plugins: [
47 {
48 resolve: "@myorg/plugin-name",
49 options: {
50 capitalize: true,
51 },
52 },
53 ],
54 })
55 ```
56
57 The `options` property in the plugin configuration is passed to all modules in a plugin.
58
59 ---
60
61 ## Access Module Options in Main Service
62
63 The module’s main service receives the module options as a second parameter.
64
65 For example:
66
67 ```ts title="src/modules/blog/service.ts" highlights={[["12"], ["14", "options?: ModuleOptions"], ["17"], ["18"], ["19"]]}
68 import { MedusaService } from "@medusajs/framework/utils"
69 import Post from "./models/post"
70
71 // recommended to define type in another file
72 type ModuleOptions = {
73 capitalize?: boolean
74 }
75
76 export default class BlogModuleService extends MedusaService({
77 Post,
78 }){
79 protected options_: ModuleOptions
80
81 constructor({}, options?: ModuleOptions) {
82 super(...arguments)
83
84 this.options_ = options || {
85 capitalize: false,
86 }
87 }
88
89 // ...
90 }
91 ```
92
93 ---
94
95 ## Access Module Options in Loader
96
97 The object that a module’s loaders receive as a parameter has an `options` property holding the module's options.
98
99 For example:
100
101 ```ts title="src/modules/blog/loaders/hello-world.ts" highlights={[["11"], ["12", "ModuleOptions", "The type of expected module options."], ["16"]]}
102 import {
103 LoaderOptions,
104 } from "@medusajs/framework/types"
105
106 // recommended to define type in another file
107 type ModuleOptions = {
108 capitalize?: boolean
109 }
110
111 export default async function helloWorldLoader({
112 options,
113 }: LoaderOptions<ModuleOptions>) {
114
115 console.log(
116 "[BLOG MODULE] Just started the Medusa application!",
117 options
118 )
119 }
120 ```
121
122 ---
123
124 ## Validate Module Options
125
126 If you expect a certain option and want to throw an error if it's not provided or isn't valid, it's recommended to perform the validation in a loader. The module's service is only instantiated when it's used, whereas the loader runs when the Medusa application starts.
127
128 So, by performing the validation in the loader, you ensure you can throw an error at an early point, rather than when the module is used.
129
130 For example, to validate that the Hello Module received an `apiKey` option, create the loader `src/modules/loaders/validate.ts`:
131
132 ```ts title="src/modules/blog/loaders/validate.ts"
133 import { LoaderOptions } from "@medusajs/framework/types"
134 import { MedusaError } from "@medusajs/framework/utils"
135
136 // recommended to define type in another file
137 type ModuleOptions = {
138 apiKey?: string
139 }
140
141 export default async function validationLoader({
142 options,
143 }: LoaderOptions<ModuleOptions>) {
144 if (!options.apiKey) {
145 throw new MedusaError(
146 MedusaError.Types.INVALID_DATA,
147 "Hello Module requires an apiKey option."
148 )
149 }
150 }
151 ```
152
153 Then, export the loader in the module's definition file, as explained in [this chapter](../loaders/page.mdx):
154
155 ```ts title="src/modules/blog/index.ts"
156 // other imports...
157 import validationLoader from "./loaders/validate"
158
159 export const BLOG_MODULE = "blog"
160
161 export default Module(BLOG_MODULE, {
162 // ...
163 loaders: [validationLoader],
164 })
165 ```
166
167 Now, when the Medusa application starts, the loader will run, validating the module's options and throwing an error if the `apiKey` option is missing.