How to understand a Medusa codebase
Medusa v2 is a commerce framework built from modules (product, cart, order and so on). Custom business logic is written as workflows made of steps with compensation for rollback, exposed through file-based API routes and triggered by events or scheduled jobs.
How to tell it's Medusa
@medusajs/*packages andmedusa-config.tssrc/workflows/,src/api/,src/modules/andsrc/subscribers/folders- Calls to
createWorkflowandcreateStep
Where things usually live
- medusa-config.ts
- Database, Redis, modules and plugins.
- src/api/**/route.ts
- File-based HTTP routes exporting
GET,POSThandlers. - src/workflows/
- Workflows and steps; the main place for business logic.
- src/modules/
- Custom modules with their own data models and services.
- src/links/
- Links between modules' data models.
- src/subscribers/
- Handlers for events such as
order.placed. - src/jobs/
- Scheduled jobs.
Conventions, not rules. Real projects rename and move things, so confirm against the repository.
Where to start reading
medusa-config.tssrc/api/routes for the feature- The workflows those routes run
How a request moves through the code
- A request reaches a route in
src/api/, after any middlewares. - The route runs a workflow with the request input.
- Each step calls module services; if a later step fails, earlier steps run their compensation functions.
- The workflow emits events, which subscribers handle asynchronously.
Questions to ask about the repository
- Which custom workflows exist, and what does each step do and undo?
- What happens after an order is placed: which subscribers run?
- Which modules are custom, and how are they linked to core modules?
What trips people up
- Medusa v1 and v2 are structurally very different; v1 patterns like
services/with a service locator don't apply. - Workflow code is a composition, not a normal function body; plain
ifstatements andawaitinsidecreateWorkflowdon't behave as you expect.