vitest-dev/vitest · error · Error

Invalid data URI encoding: ${encoding}

Error message

Invalid data URI encoding: ${encoding}

What it means

Thrown by createDataModule when a `data:application/wasm` URI has an encoding segment that is not `base64`. The wasm branch only accepts base64 (line 215-216); any other encoding value such as `charset=utf-8` is rejected because wasm bytes cannot be represented that way through this path.

Source

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

      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,
      )
      this.moduleCache.set(identifier, module)
      return module
    }

    let code = match.groups.code
    if (!encoding || encoding === 'charset=utf-8') {
      code = decodeURIComponent(code)
    }
    else if (encoding === 'base64') {
      code = Buffer.from(code, 'base64').toString()
    }
    else {

View on GitHub (pinned to d568f8ce37)

Solutions

  1. Use `;base64` for wasm data URIs and base64-encode the binary payload.
  2. Remove a stray charset=utf-8 from the wasm URI.
  3. Load wasm from a fixture file instead of a data URI.

Example fix

// before
import wasm from 'data:application/wasm;charset=utf-8,AGFzbQ'

// after
import wasm from 'data:application/wasm;base64,AGFzbQ=='
Defensive patterns

Strategy: validation

Validate before calling

function assertWasmEncoding(uri: string) {
  const m = uri.match(/^data:application\/wasm;([^,]+),/)
  if (m && m[1] !== 'base64') throw new Error(`wasm data URI encoding must be base64, got ${m[1]}`)
}

Prevention

When it happens

Trigger: Importing `data:application/wasm;charset=utf-8,<text>` or `data:application/wasm;charset=ascii,<text>`. The encoding group matched but its value is not base64.

Common situations: Defaulting to charset=utf-8 for all data URIs including wasm. A template that appends charset=utf-8 unconditionally. Copying a JS/JSON data URI template for wasm.

Related errors


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