twbs/bootstrap · error · TypeError

No method named "${config}"

Error message

No method named "${config}"

What it means

Carousel's jQueryInterface treats a numeric argument specially — data.to(config) jumps to that slide — and only string arguments go through method dispatch, which rejects names that do not resolve on the instance, start with '_', or equal 'constructor'. Public methods are next, nextWhenVisible, prev, pause, cycle, to and dispose. A slide number sent as a string ('2') bypasses the numeric path, fails the lookup, and throws.

Source

Thrown at js/src/carousel.js:419

      return order === ORDER_PREV ? DIRECTION_LEFT : DIRECTION_RIGHT
    }

    return order === ORDER_PREV ? DIRECTION_RIGHT : DIRECTION_LEFT
  }

  // Static
  static jQueryInterface(config) {
    return this.each(function () {
      const data = Carousel.getOrCreateInstance(this, config)

      if (typeof config === 'number') {
        data.to(config)
        return
      }

      if (typeof config === 'string') {
        if (data[config] === undefined || config.startsWith('_') || config === 'constructor') {
          throw new TypeError(`No method named "${config}"`)
        }

        data[config]()
      }
    })
  }
}

/**
 * Data API implementation
 */

EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_SLIDE, function (event) {
  const target = SelectorEngine.getElementFromSelector(this)

  if (!target || !target.classList.contains(CLASS_NAME_CAROUSEL)) {
    return
  }

View on GitHub (pinned to 6177d5f849)

Solutions

  1. To jump to a slide, pass a number: $('#carousel').carousel(2) — it maps to data.to(2).
  2. Use only public methods: next, nextWhenVisible, prev, pause, cycle, to, dispose.
  3. Convert string indexes before the call: Number(index) if !Number.isNaN(Number(index)).
  4. For dynamic names, check typeof instance[name] === 'function' && !name.startsWith('_') && name !== 'constructor' first.

Example fix

// before
$('#carousel').carousel('2')       // string → method lookup → TypeError
$('#carousel').carousel('goTo', 2) // no such method

// after
$('#carousel').carousel(2)         // number → data.to(2)
$('#carousel').carousel('next')    // next|nextWhenVisible|prev|pause|cycle|to|dispose
Defensive patterns

Strategy: validation

Validate before calling

function callCarouselMethod(element, arg) {
  const instance = bootstrap.Carousel.getOrCreateInstance(element)
  if (typeof arg === 'number') { // numeric shortcut → data.to(arg)
    instance.to(arg)
    return
  }
  const callable = typeof instance[arg] === 'function' &&
    !arg.startsWith('_') && arg !== 'constructor'
  if (callable) instance[arg]()
  else console.warn(`Carousel: ignoring unknown method "${arg}"`)
}

Type guard

const CAROUSEL_METHODS = ['next', 'nextWhenVisible', 'prev', 'pause', 'cycle', 'to', 'dispose'] as const
type CarouselMethod = (typeof CAROUSEL_METHODS)[number]
const isCarouselMethod = (m: string): m is CarouselMethod =>
  (CAROUSEL_METHODS as readonly string[]).includes(m)

Try / catch

try {
  $('#carousel').carousel(method)
} catch (err) {
  if (err instanceof TypeError && err.message.startsWith('No method named')) {
    console.warn(`Carousel: unknown method "${method}"`)
  } else {
    throw err
  }
}

Prevention

When it happens

Trigger: $('#carousel').carousel('2') — the slide index as a string instead of the number 2; $('#carousel').carousel('goTo', 2); $('#carousel').carousel('slide') (a common guess, not a v5 method); $('#carousel').carousel('_config') or 'constructor'.

Common situations: Slide indexes arriving from templates/routes as strings; code migrated from v4 where the jQuery API differed; automated tests exercising the jQuery surface with typo'd or guessed method names.

Related errors


AI-assisted analysis of twbs/bootstrap@6177d5f849 (2026-08-22). Data as JSON: /api/errors/edbdcea1735d3d06. Report an issue: GitHub.