Skill 14 · Building With Medusa
Subchapter 14.11
reference/troubleshooting.mdMarkdown5 KBView on GitHub
This guide covers common errors and their solutions when building with Medusa.
Error: Module "my-module" is not registered in the containerCause: Module not added to medusa-config.ts or server not restarted.
Solution:
medusa-config.ts:module.exports = defineConfig({
modules: [
{ resolve: "./src/modules/my-module" }
],
})Error: Cannot find module './modules/my-module'Cause: Module path is incorrect or module structure is incomplete.
Solution:
src/modules/my-module/
├── models/
│ └── my-model.ts
├── service.ts
└── index.tsindex.ts exports the module correctlymedusa-config.ts matches actual directoryTypeError: Cannot read property 'email' of undefinedCause: Forgot to add validation middleware or accessing req.validatedBody instead of req.body.
Solution:
// middlewares.ts
export const myMiddlewares: MiddlewareRoute[] = [
{
matcher: "/store/my-route",
method: "POST",
middlewares: [validateAndTransformBody(MySchema)],
},
]req.validatedBody not req.bodyTypeError: Cannot spread undefinedCause: Using ...req.queryConfig without setting up query config middleware.
Solution:
Add validateAndTransformQuery middleware:
import { createFindParams } from "@medusajs/medusa/api/utils/validators"
export const GetMyItemsSchema = createFindParams()
export default defineMiddlewares({
routes: [
{
matcher: "/store/my-items",
method: "GET",
middlewares: [
validateAndTransformQuery(GetMyItemsSchema, {
defaults: ["id", "name"],
isList: true,
}),
],
},
],
})Error: [object Object]Cause: Throwing regular Error instead of MedusaError.
Solution:
// ❌ WRONG
throw new Error("Not found")
// ✅ CORRECT
import { MedusaError } from "@medusajs/framework/utils"
throw new MedusaError(MedusaError.Types.NOT_FOUND, "Not found")Error: Route is not being validatedCause: Middleware matcher doesn’t match route path or middleware not registered.
Solution:
// For route: /store/my-route
matcher: "/store/my-route" // Exact match
// For multiple routes: /store/my-route, /store/my-route/123
matcher: "/store/my-route*" // Wildcardapi/middlewares.tsTypeError: Cannot read property 'actor_id' of undefinedCause: Route is not protected or user is not authenticated.
Solution:
/admin/* or /store/customers/me/*)export default defineMiddlewares({
routes: [
{
matcher: "/custom/admin*",
middlewares: [authenticate("user", ["session", "bearer", "api-key"])],
},
],
})auth_context exists:const userId = req.auth_context?.actor_id
if (!userId) {
// Handle unauthenticated case
}# Set log level to debug
LOG_LEVEL=debug npx medusa developimport {
createStep,
createWorkflow,
StepResponse,
WorkflowResponse,
transform,
} from "@medusajs/framework/workflows-sdk"
const step1 = createStep(
"step-1",
async () => {
const message = "Hello from step 1!"
return new StepResponse(
message
)
}
)
export const myWorkflow = createWorkflow(
"my-workflow",
() => {
const response = step1()
const transformedMessage = transform(
{ response },
(data) => {
const upperCase = data.response.toUpperCase()
console.log("Transformed Data:", upperCase)
return upperCase
}
)
return new WorkflowResponse({
response: transformedMessage,
})
}
)