Install Product Module in Medusa
In this document, you'll learn how to install the Product module using NPM in the Medusa backend.
Step 1: Install Module
To install the Product module, run the following command in the root directory of the Medusa backend:
Step 2: Add Module to Configurations
In medusa-config.js
, add the product module to the exported object under the modules
property:
medusa-config.js
Step 3: Run Migrations
Run the following command to reflect schema changes into your database:
Use the Module
You can now start using the module's ProductModuleService
by resolving it through dependency injection.
For example:
import type {
MedusaRequest,
MedusaResponse
} from "@medusajs/medusa";
import {
ProductModuleService
} from "@medusajs/product"
export async function GET(
req: MedusaRequest,
res: MedusaResponse
) {
const productModuleService: ProductModuleService =
req.scope.resolve(
"productModuleService"
)
return res.json({
products: productModuleService.list()
})
}
import {
type SubscriberConfig,
type SubscriberArgs,
ProductService,
} from "@medusajs/medusa"
import {
ProductModuleService
} from "@medusajs/product"
export default async function handleListProducts({
data, eventName, container, pluginOptions
}: SubscriberArgs<Customer>) {
const productModuleService: ProductModuleService =
container.resolve(
"productModuleService"
)
console.log(await productModuleService.list())
}
export const config: SubscriberConfig = {
event: ProductService.Events.CREATED,
context: {
subscriberId: "list-products"
}
}
import { TransactionBaseService } from "@medusajs/medusa"
import {
ProductModuleService
} from "@medusajs/product"
class HelloService extends TransactionBaseService {
private productModuleService: ProductModuleService
constructor(container) {
super(container)
this.productModuleService = container.productModuleService
}
await listProducts() {
return await this.productModuleService.list()
}
}
export default HelloService
Start Development
You can refer to the Example Usages documentation page for examples of using the Product module.
You can also refer to the Module Interface Reference for a detailed reference on all available methods.
Was this section helpful?