API Route Registration Error

If your Medusa application starts successfully but API routes fail to register, you may encounter the following runtime error:

Cannot read properties of undefined (reading 'def')

Why This Error Occurred#

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:

  • Missing named exports for HTTP methods (for example, no export const GET = ... in the file).
  • A route handler variable is referenced but not defined or imported correctly.
  • The exported value is not a function.

Medusa expects each API route file to export named functions for the HTTP methods it handles. For example:

src/api/store/custom/route.ts
1import {2  MedusaRequest,3  MedusaResponse,4} from "@medusajs/framework/http"5
6export const GET = async (7  req: MedusaRequest,8  res: MedusaResponse9) => {10  res.json({ message: "Hello" })11}

How to Fix It#

Check all files in your src/api directory and ensure that:

  1. Each route file exports at least one named HTTP method handler (GET, POST, PUT, PATCH, or DELETE).
  2. Each exported handler is a function, not undefined.
  3. Any imported helper functions referenced in your exports are defined and exported correctly from their source file.

You can check your runtime logs to identify which route file caused the error.

Learn more in the API Routes documentation.

Was this page helpful?
Ask Bloom
For assistance in your development, use Claude Code Plugins or Docs MCP server in Cursor, VSCode, etc...FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break
⇧↵