jestjs/jest · error · Error
Invalid data URI
Error message
Invalid data URI
What it means
Thrown by parseDataUri when a `data:` URI specifier passed to the ESM loader does not match the strict regex `^data:(text/javascript|application/json|application/wasm)(;charset=utf-8|;base64)?,(.*)$`. Only those three MIME types with optional charset/base64 encoding are accepted.
Source
Thrown at packages/jest-runtime/src/internals/EsmLoader.ts:136
const error: NodeJS.ErrnoException = new Error(
`require() cannot be used to load ES Module ${modulePath}: ${detail}`,
);
error.code = 'ERR_REQUIRE_ASYNC_MODULE';
return error;
}
// Decode a `data:` URI specifier into its mime type and decoded code/body.
// `application/wasm` returns a Buffer; everything else returns a UTF-8 string.
const dataURIRegex =
/^data:(?<mime>text\/javascript|application\/json|application\/wasm)(?:;(?<encoding>charset=utf-8|base64))?,(?<code>.*)$/;
function parseDataUri(specifier: string): {
mime: string;
code: string | Buffer;
} {
const match = specifier.match(dataURIRegex);
if (!match || !match.groups) {
throw new Error('Invalid data URI');
}
const {mime, encoding, code} = match.groups;
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 {code: Buffer.from(code, 'base64'), mime};
}
if (!encoding || encoding === 'charset=utf-8') {
return {code: decodeURIComponent(code), mime};
}
if (encoding === 'base64') {
return {code: Buffer.from(code, 'base64').toString(), mime};
}
throw new Error(`Invalid data URI encoding: ${encoding}`);
}
View on GitHub (pinned to f49721c78e)
Solutions
- Use `data:text/javascript,...` for JS, `data:application/json,...` for JSON, `data:application/wasm;base64,...` for WASM.
- Ensure the URI has a comma separating the header from the body.
- Strip any whitespace or quotes around the specifier before importing.
- URL-encode the body with `encodeURIComponent` for the charset=utf-8 path.
Example fix
// before import code from 'data:application/javascript,export const x = 1;'; // after import code from 'data:text/javascript,export%20const%20x%20%3D%201;';
Defensive patterns
Strategy: validation
Validate before calling
const DATA_URI_RE = /^data:(text\/javascript|application\/json|application\/wasm)(;charset=utf-8|;base64)?,(.*)$/;
function isValidDataUri(uri) {
return DATA_URI_RE.test(uri);
}
// use: if (!isValidDataUri(specifier)) throw new Error('Bad data URI'); Type guard
function isDataUriSpecifier(s: string): boolean {
return /^data:(text\/javascript|application\/json|application\/wasm)(;charset=utf-8|;base64)?,/.test(s);
} Prevention
- Use only the three accepted MIME types: text/javascript, application/json, application/wasm.
- Build data URIs with a helper rather than hand-writing them.
- URL-encode text bodies with encodeURIComponent.
When it happens
Trigger: An ESM import using a `data:` URI whose MIME type is wrong (e.g. `data:text/plain,...` or `data:application/javascript,...` — note the regex requires `text/javascript`), or that omits the comma, or uses an unsupported encoding token. Triggered when a test imports a module via a data URI string.
Common situations: Using `data:application/javascript` instead of `data:text/javascript` (the spec-accepted MIME); forgetting the comma separator; pasting a data URI from a browser context that uses a different MIME; trailing whitespace or characters breaking the regex anchor.
Related errors
- Missing data URI encoding
- Invalid data URI encoding: ${encoding}
- expected value must be a function
- jest.config.mts requires native TypeScript support. Ensure y
- Do not import `@jest/globals` outside of the Jest test envir
AI-assisted analysis of jestjs/jest@f49721c78e (2026-08-03).
Data as JSON: /data/errors/074e15b87e21989e.json.
Report an issue: GitHub.