vitest-dev/vitest · error · Error

Invalid data URI

Error message

Invalid data URI

What it means

Thrown by EsmExecutor.createDataModule when a `data:` URI identifier does not match the supported regex (which allows mime text/javascript, application/json, or application/wasm, optionally with charset=utf-8 or base64, followed by the payload). Any data URI with an unsupported mime, a malformed structure, or no captured groups fails here.

Source

Thrown at packages/vitest/src/runtime/vm/esm-executor.ts:204

  public cacheModule(identifier: string, module: VMModule): void {
    this.moduleCache.set(identifier, module)
  }

  public resolveCachedModule(identifier: string): VMModule | Promise<VMModule> | undefined {
    return this.moduleCache.get(identifier)
  }

  public async createDataModule(identifier: string): Promise<VMModule> {
    const cached = this.moduleCache.get(identifier)
    if (cached) {
      return cached
    }

    const match = identifier.match(dataURIRegex)

    if (!match || !match.groups) {
      throw new Error('Invalid data URI')
    }

    const mime = match.groups.mime
    const encoding = match.groups.encoding

    if (mime === 'application/wasm') {
      if (!encoding) {
        throw new Error('Missing data URI encoding')
      }

      if (encoding !== 'base64') {
        throw new Error(`Invalid data URI encoding: ${encoding}`)
      }

      const module = this.loadWebAssemblyModule(
        Buffer.from(match.groups.code, 'base64'),
        identifier,
      )

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Use one of the supported MIME types: `text/javascript`, `application/json`, or `application/wasm`.
  2. Ensure the URI is well-formed: `data:<mime>[;<encoding>],<payload>`.
  3. For JavaScript, prefer `data:text/javascript,console.log(1)` (plain) or `;base64,` form.
  4. Move the payload into a fixture file and import it normally if the data URI keeps failing.

Example fix

// before
import code from 'data:application/javascript,console.log(1)'

// after
import code from 'data:text/javascript,console.log(1)'
Defensive patterns

Strategy: validation

Validate before calling

const DATA_URI = /^data:(?:text\/javascript|application\/json|application\/wasm)(?:;(?:charset=utf-8|base64))?,.*$/
function isValidDataUri(uri: string): boolean {
  return DATA_URI.test(uri)
}

Prevention

When it happens

Trigger: Importing a `data:` URI whose MIME type is not one of text/javascript, application/json, or application/wasm (e.g. `data:text/plain,...` or `data:application/javascript,...` - note the regex requires exactly text/javascript). A URI missing the `data:` scheme or the comma. A URI with a mime but empty payload that still fails the regex.

Common situations: Using a non-standard mime like `application/javascript` (the regex only allows `text/javascript`). Constructing data URIs by hand and getting the mime wrong. Importing JSON with `data:application/json` but missing the comma/payload.

Related errors


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/75a0b833ff6d2b95.json. Report an issue: GitHub.