gatsbyjs/gatsby · error · Error

The value for the $regex comparator must be an instance of R

Error message

The value for the $regex comparator must be an instance of RegExp

What it means

getNodesFromCacheByValue handling of the $regex (and converted $glob) operator requires the filter value to be a RegExp instance so it can match against the indexed bucket values; a non-RegExp value reaching this branch trips this guard.

Source

Thrown at packages/gatsby/src/datastore/in-memory/indexing.ts:780

  if (op === `$ne`) {
    const set = new Set(filterCache.meta.nodesUnordered)

    removeBucketFromSet(filterValue, filterCache, set)

    // TODO: there's probably a more efficient algorithm to do set
    //       subtraction in such a way that we don't have to resort here
    return [...set].sort(sortByIds)
  }

  if (op === `$regex`) {
    // Note: $glob is converted to $regex so $glob filters go through here, too
    // Aside from the input pattern format, further behavior is exactly the same.

    // The input to the filter must be a string (including leading/trailing slash and regex flags)
    // By the time the filter reaches this point, the filterValue has to be a regex.

    if (!(filterValue instanceof RegExp)) {
      throw new Error(
        `The value for the $regex comparator must be an instance of RegExp`
      )
    }
    const regex = filterValue

    const arr: Array<IGatsbyNodePartial> = []
    filterCache.byValue.forEach((nodes, value) => {
      // TODO: does the value have to be a string for $regex? Can we auto-ignore any non-strings? Or does it coerce.
      // Note: for legacy reasons partial paths should also be included for regex
      if (value !== undefined && regex.test(String(value))) {
        nodes.forEach(node => arr.push(node))
      }
    })

    // TODO: we _can_ cache this list as well. Might make sense if it turns out that $regex is mostly used with literals
    // TODO: it may make sense to first collect all buckets and then to .concat them, or merge sort them

    arr.sort(sortByIds)

View on GitHub (pinned to e85d62f177)

Solutions

  1. Pass a RegExp instance as the value for $regex
  2. For $glob, supply a glob string; it is converted to RegExp internally
  3. Do not send plain strings to $regex in custom API usage
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at packages/gatsby/src/datastore/in-memory/indexing.ts:780 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of gatsbyjs/gatsby@e85d62f177 (2026-08-26). Data as JSON: /api/errors/e0f0c74cf8a1a6cc. Report an issue: GitHub.