vitejs/vite · error · Error

offset is longer than source length! offset ${offset} > leng

Error message

offset is longer than source length! offset ${offset} > length ${source.length}

What it means

Thrown by numberToPos() when the supplied character offset exceeds the source string length. numberToPos converts a numeric offset to a {line, column} position; an out-of-range offset points to a bug in whatever produced the offset (sourcemap, plugin, error overlay).

Source

Thrown at packages/vite/src/node/utils.ts:510

  /** 0-based */
  column: number
}

export function posToNumber(source: string, pos: number | Pos): number {
  if (typeof pos === 'number') return pos
  const lines = source.split(splitRE)
  const { line, column } = pos
  let start = 0
  for (let i = 0; i < line - 1 && i < lines.length; i++) {
    start += lines[i].length + 1
  }
  return start + column
}

export function numberToPos(source: string, offset: number | Pos): Pos {
  if (typeof offset !== 'number') return offset
  if (offset > source.length) {
    throw new Error(
      `offset is longer than source length! offset ${offset} > length ${source.length}`,
    )
  }

  const lines = source.slice(0, offset).split(splitRE)
  return {
    line: lines.length,
    column: lines[lines.length - 1].length,
  }
}

const MAX_DISPLAY_LEN = 120
const ELLIPSIS = '...'

export function generateCodeFrame(
  source: string,
  start: number | Pos = 0,
  end?: number | Pos,

View on GitHub (pinned to b4d66fee14)

Solutions

  1. Clear Vite's cache (node_modules/.vite) and restart the dev server to drop stale sourcemaps/ASTs.
  2. Identify the plugin doing source-position reporting (search stack trace for numberToPos / generateCodeFrame callers) and check it recomputes positions after transforms.
  3. Verify the failing file isn't being transformed by two plugins that disagree on source length.
  4. Report a bug with a reproduction if it persists on a clean cache — this usually indicates an internal position-tracking defect.
Defensive patterns

Strategy: validation

Validate before calling

if (offset > source.length) {
  throw new RangeError(`offset ${offset} out of bounds for length ${source.length}`)
}

Prevention

When it happens

Trigger: A plugin, sourcemap, or error-reporting path calling numberToPos(source, offset) where offset > source.length. Guard at utils.ts:509. Often surfaced via generateCodeFrame or error overlay rendering after a transform.

Common situations: A sourcemap mapping points past EOF after a transform mutated code length. A plugin returns a position from a stale AST after the source changed (HMR partial update). Minified/bundled output whose sourcemap offsets don't match. Outdated Vite cache after editing a file.

Related errors


AI-assisted analysis of vitejs/vite@b4d66fee14 (2026-08-11). Data as JSON: /api/errors/9ebba2cf750f3f8b. Report an issue: GitHub.