medusajs/medusa · error · MedusaError

"shipping_methods" should be present as an array in the cont

Error message

"shipping_methods" should be present as an array in the context for computeActions

What it means

getComputedActionsForShippingMethods requires the shipping_methods array in the computeActions context when computing adjustments for shipping-targeted promotions. A missing/null shippingMethodApplicationContext throws INVALID_DATA.

Source

Thrown at packages/modules/promotion/src/utils/compute-actions/shipping-methods.ts:28

  ComputedActions,
  MathBN,
  MedusaError,
} from "@medusajs/framework/utils"
import { Promotion } from "@models"
import { areRulesValidForContext } from "../validations"
import { sortShippingLineByPriceAscending } from "./sort-by-price"
import { computeActionForBudgetExceeded } from "./usage"

export function getComputedActionsForShippingMethods(
  promotion: PromotionTypes.PromotionDTO | InferEntityType<typeof Promotion>,
  shippingMethodApplicationContext: PromotionTypes.ComputeActionContext[ApplicationMethodTargetType.SHIPPING_METHODS],
  methodIdPromoValueMap: Map<string, number>
): PromotionTypes.ComputeActions[] {
  let applicableShippingItems: PromotionTypes.ComputeActionContext[ApplicationMethodTargetType.SHIPPING_METHODS] =
    []

  if (!shippingMethodApplicationContext) {
    throw new MedusaError(
      MedusaError.Types.INVALID_DATA,
      `"shipping_methods" should be present as an array in the context for computeActions`
    )
  }

  for (const shippingMethodContext of shippingMethodApplicationContext) {
    const isPromotionApplicableToItem = areRulesValidForContext(
      promotion.application_method?.target_rules!,
      shippingMethodContext,
      ApplicationMethodTargetType.SHIPPING_METHODS
    )

    if (!isPromotionApplicableToItem) {
      continue
    }

    applicableShippingItems.push(shippingMethodContext)
  }

View on GitHub (pinned to 5e06e544a2)

Solutions

  1. Include shipping_methods: [] (or the mapped cart shipping methods) in the context whenever shipping-targeted codes may be computed
  2. Re-run computeActions after the customer selects a shipping method
  3. Filter passed promotion codes by their target_type to match the context you have

Example fix

// before
await promotionModule.computeActions(["FREESHIP"], {
  items: mapItems(cart.items),
})

// after
await promotionModule.computeActions(["FREESHIP"], {
  items: mapItems(cart.items),
  shipping_methods: cart.shipping_methods.map((m) => ({
    id: m.id,
    amount: m.amount,
  })),
})
Defensive patterns

Strategy: validation

Validate before calling

const context = { items: mapItems(cart.items), shipping_methods: cart.shipping_methods?.map((m) => ({ id: m.id, amount: m.amount })) ?? [] }

Type guard

const hasShippingContext = (ctx: unknown): boolean => Array.isArray((ctx as any)?.shipping_methods)

Try / catch

try { await computeActions(codes, context) } catch (e) { if (e.type === MedusaError.Types.INVALID_DATA && /shipping_methods/.test(e.message)) { /* defer computation until shipping is selected */ } throw e }

Prevention

When it happens

Trigger: Calling computeActions for a promotion whose application_method.target_type is 'shipping_methods' while the context omits the shipping_methods key (e.g., only items provided), or passes it as null.

Common situations: Free-shipping promo codes applied to carts before shipping methods are chosen (context built without shipping data), checkout step ordering — computing adjustments too early, context builders that skip empty shipping arrays.

Related errors


AI-assisted analysis of medusajs/medusa@5e06e544a2 (2026-08-27). Data as JSON: /api/errors/db7af90a931d5aa0. Report an issue: GitHub.