|
1
|
If your Medusa application starts successfully but API routes fail to register, you may encounter the following runtime error: |
|
2
|
|
|
3
|
``` |
|
4
|
Cannot read properties of undefined (reading 'def') |
|
5
|
``` |
|
6
|
|
|
7
|
## Why This Error Occurred |
|
8
|
|
|
9
|
This error occurs when a file in `src/api` exports a route handler that is `undefined` or has an incorrect structure. The most common causes are: |
|
10
|
|
|
11
|
- Missing named exports for HTTP methods (for example, no `export const GET = ...` in the file). |
|
12
|
- A route handler variable is referenced but not defined or imported correctly. |
|
13
|
- The exported value is not a function. |
|
14
|
|
|
15
|
Medusa expects each API route file to export named functions for the HTTP methods it handles. For example: |
|
16
|
|
|
17
|
```ts title="src/api/store/custom/route.ts" |
|
18
|
import { |
|
19
|
MedusaRequest, |
|
20
|
MedusaResponse, |
|
21
|
} from "@medusajs/framework/http" |
|
22
|
|
|
23
|
export const GET = async ( |
|
24
|
req: MedusaRequest, |
|
25
|
res: MedusaResponse |
|
26
|
) => { |
|
27
|
res.json({ message: "Hello" }) |
|
28
|
} |
|
29
|
``` |
|
30
|
|
|
31
|
--- |
|
32
|
|
|
33
|
## How to Fix It |
|
34
|
|
|
35
|
Check all files in your `src/api` directory and ensure that: |
|
36
|
|
|
37
|
1. Each route file exports at least one named HTTP method handler (`GET`, `POST`, `PUT`, `PATCH`, or `DELETE`). |
|
38
|
2. Each exported handler is a function, not `undefined`. |
|
39
|
3. Any imported helper functions referenced in your exports are defined and exported correctly from their source file. |
|
40
|
|
|
41
|
You can check your runtime logs to identify which route file caused the error. |
|
42
|
|
|
43
|
Learn more in the [API Routes](!docs!/learn/fundamentals/api-routes) documentation. |