import DocCard from '@theme/DocCard'
import Icons from '@theme/Icon'
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# 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:

```bash npm2yarn
npm install @medusajs/product
```

---

## Step 2: Add Module to Configurations

In `medusa-config.js`, add the product module to the exported object under the `modules` property:

```js title=medusa-config.js
module.exports = {
  // ...
  modules: {
    // ...
    productService: {
      resolve: "@medusajs/product",
    },
  },
}
```

---

## Step 3: Run Migrations

Run the following command to reflect schema changes into your database:

```bash
npx medusa migrations run
```

---

## Use the Module

You can now start using the module's `ProductModuleService` by resolving it through [dependency injection](../../development/fundamentals/dependency-injection.md).

For example:

<Tabs groupId="resource-type" isCodeTabs={true}>
  <TabItem value="api-route" label="API Route" attributes={{
    title: "src/api/store/custom/route.ts"
  }} default>

    ```ts
    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()
      })
    }
    ```

  </TabItem>
  <TabItem value="subscribers" label="Subscribers" attributes={{
    title: "src/subscribers/create-customer.ts"
  }}>

    ```ts
    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"
      }
    }
    ```

  </TabItem>
  <TabItem value="service" label="Service" attributes={{
    title: "src/service/hello.ts"
  }}>

    ```ts
    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
    ```

  </TabItem>
</Tabs>

---

## Start Development

You can refer to the [Example Usages documentation page](./examples.mdx) for examples of using the Product module.

You can also refer to the [Module Interface Reference](../../references/product/interfaces/product.IProductModuleService.mdx) for a detailed reference on all available methods.