vitest-dev/vitest · error · Error
Invalid data URI encoding
Error message
Invalid data URI encoding: ${encoding} What it means
Thrown by parseDataUri (line 48) only for WebAssembly data URIs: the MIME is `application/wasm`, an encoding IS present, but it is not `base64`. Vitest supports exclusively base64-encoded wasm data URIs, so any other encoding value (e.g. `charset=utf-8`) for wasm is rejected here, before attempting to decode binary.
Solutions
- Switch the encoding to base64 for wasm: `data:application/wasm;base64,...`.
- If you have raw text you think is wasm, you likely need real binary bytes base64-encoded, not a text encoding.
- Load wasm from a file URL to avoid encoding pitfalls entirely.
Example fix
// before import wasm from 'data:application/wasm;charset=utf-8,...' // after import wasm from 'data:application/wasm;base64,<base64>'
Defensive patterns
Strategy: validation
Validate before calling
function validWasmDataUri(uri: string): boolean {
return /^data:application\/wasm;base64,.+$/.test(uri)
} Type guard
function isBase64WasmDataUri(uri: string): boolean {
return /^data:application\/wasm;base64,[A-Za-z0-9+/=]+$/.test(uri)
} Prevention
- For wasm data URIs, use base64 exclusively.
- Reject non-base64 encodings early in URI-building helpers.
- Document the wasm-must-be-base64 rule in any asset-emitting plugin.
When it happens
Trigger: Importing a wasm module via a data URI with a non-base64 encoding segment, e.g. `data:application/wasm;charset=utf-8,...`. This is invalid because wasm bytes cannot be represented as UTF-8 text in a URI.
Common situations: Mis-generating a wasm URI with the wrong encoding token; concatenating a JS-style `charset=utf-8` header onto a wasm payload; tooling that defaults all data URIs to the same encoding.
Related errors
- Missing data URI encoding
- Invalid data URI
- Cannot import " ": its vm context was torn down.
- invalid diff config file
- invalid snapshot serializer file
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/9852ea8a9055fad6.
Report an issue: GitHub.
Appendix: source
Thrown at packages/vitest/src/runtime/vm/esm-executor.ts:48
return module instanceof SourceTextModule && module.hasAsyncGraph()
}
const dataURIRegex
= /^data:(?<mime>text\/javascript|application\/json|application\/wasm)(?:;(?<encoding>charset=utf-8|base64))?,(?<code>.*)$/
function parseDataUri(identifier: string): { mime: string; code: string | Buffer } {
const match = identifier.match(dataURIRegex)
if (!match || !match.groups) {
throw new Error('Invalid data URI')
}
const { mime, encoding } = match.groups
let code: string | Buffer = match.groups.code
if (mime === 'application/wasm') {
if (!encoding) {
throw new Error('Missing data URI encoding')
}
if (encoding !== 'base64') {
throw new Error(`Invalid data URI encoding: ${encoding}`)
}
return { mime, code: Buffer.from(code, 'base64') }
}
if (!encoding || encoding === 'charset=utf-8') {
code = decodeURIComponent(code)
}
else if (encoding === 'base64') {
code = Buffer.from(code, 'base64').toString()
}
else {
throw new Error(`Invalid data URI encoding: ${encoding}`)
}
return { mime, code }
}
function getContextExecutor(mod: VMModule): ExternalModulesExecutor {
const vmContext = (mod.context as any)?.[VITEST_VM_CONTEXT_SYMBOL]
if (!vmContext) {View on GitHub (pinned to 1fa9837ec2)