1 # Product Module
2
3 The Product Module gives you access Products, Variants, Categories, and more through a standalone package that can be installed and run in Next.js functions and other Node.js compatible runtimes.
4
5 [Product Module documentation](https://docs.medusajs.com/v2/resources/commerce-modules/product) | [Medusa Website](https://medusajs.com/) | [Medusa Repository](https://github.com/medusajs/medusa)
6
7 > The Product Module is currently in beta. The beta version comes with limited functionality, primarily centered around retrieving products. In the official version, the product module will be fully-fledged and on par with the product functionality in our core package.
8
9 ---
10
11 ## Installing and using it in Next.js
12
13 ### Prerequisites
14
15 - [Next.js project](https://nextjs.org/docs/pages/api-reference/create-next-app)
16
17 1\. Run the following command in your project
18
19 ```bash
20 npm install @medusajs/product
21 ```
22
23 2\. Add Database URL to your environment variables
24
25 ```bash
26 DATABASE_URL=<URL_GOES_HERE>
27 ```
28
29 3\. Apply database migrations
30
31 > If you are using an existing Medusa database, you can skip this step. This step is only applicable when the module is used in isolation from a full Medusa setup
32
33 Before you can run migrations, add in your `package.json` the following scripts:
34
35 ```json
36 "scripts": {
37 //...other scripts
38 "product:migrations:run": "medusa-product-migrations-up",
39 "product:seed": "medusa-product-seed ./seed-data.js"
40 },
41 ```
42
43 The first command runs the migrations, and the second command allows you to optionally seed your database with demo products.
44
45 For the second command to work, you'll need to add the dummy seed data to the root of your Next.js project:
46
47 <details>
48 <summary>Seed file</summary>
49
50 ```js
51 const productCategoriesData = [
52 {
53 id: "category-0",
54 name: "category 0",
55 parent_category_id: null,
56 },
57 {
58 id: "category-1",
59 name: "category 1",
60 parent_category_id: "category-0",
61 },
62 {
63 id: "category-1-a",
64 name: "category 1 a",
65 parent_category_id: "category-1",
66 },
67 {
68 id: "category-1-b",
69 name: "category 1 b",
70 parent_category_id: "category-1",
71 is_internal: true,
72 },
73 {
74 id: "category-1-b-1",
75 name: "category 1 b 1",
76 parent_category_id: "category-1-b",
77 },
78 ]
79
80 const productsData = [
81 {
82 id: "test-1",
83 title: "product 1",
84 status: "published",
85 descriptions: "Lorem ipsum dolor sit amet, consectetur.",
86 tags: [
87 {
88 id: "tag-1",
89 value: "France",
90 },
91 ],
92 categories: [
93 {
94 id: "category-0",
95 },
96 ],
97 },
98 {
99 id: "test-2",
100 title: "product",
101 status: "published",
102 descriptions: "Lorem ipsum dolor sit amet, consectetur.",
103 tags: [
104 {
105 id: "tag-2",
106 value: "Germany",
107 },
108 ],
109 categories: [
110 {
111 id: "category-1",
112 },
113 ],
114 },
115 ]
116
117 const variantsData = [
118 {
119 id: "test-1",
120 title: "variant title",
121 sku: "sku 1",
122 product: { id: productsData[0].id },
123 },
124 {
125 id: "test-2",
126 title: "variant title",
127 sku: "sku 2",
128 product: { id: productsData[1].id },
129 },
130 ]
131
132 module.exports = {
133 productCategoriesData,
134 productsData,
135 variantsData,
136 }
137 ```
138
139 </details>
140
141 Then run the first and optionally the second command to migrate and seed the database:
142
143 ```bash
144 npm run product:migrations:run
145 # optionally
146 npm run product:seed
147 ```
148
149 4\. Adjust Next.js config
150
151 Next.js uses Webpack for compilation. Since quite a few of the dependencies used by the product module are not Webpack optimized, you have to add the product module as an external dependency.
152
153 To do that, add the serverComponentsExternalPackages option in `next.config.js`:
154
155 ```js
156 /** @type {import('next').NextConfig} */
157
158 const nextConfig = {
159 experimental: {
160 serverComponentsExternalPackages: ["@medusajs/product"],
161 },
162 }
163
164 module.exports = nextConfig
165 ```
166
167 5\. Create API Route
168
169 The product module is ready for use now! You can now use it to create API endpoints within your Next.js application.
170
171 For example, create the file `app/api/products/route.ts` with the following content:
172
173 ```ts
174 import { NextResponse } from "next/server"
175
176 import { initialize as initializeProductModule } from "@medusajs/product"
177
178 export async function GET(request: Request) {
179 const productService = await initializeProductModule()
180
181 const data = await productService.list()
182
183 return NextResponse.json({ products: data })
184 }
185 ```
186
187 6\. Test your Next.js application
188 To test the endpoint you added, start your Next.js application with the following command:
189
190 ```
191 npm run dev
192 ```
193
194 Then, open in your browser the URL `http://localhost:3000/api/products`. If you seeded your database with demo products, or you’re using a Medusa database schema, you’ll receive the products in your database. Otherwise, the request will return an empty array.