{"id":"074e15b87e21989e","repo":"jestjs/jest","slug":"invalid-data-uri","errorCode":null,"errorMessage":"Invalid data URI","messagePattern":"Invalid data URI","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/jest-runtime/src/internals/EsmLoader.ts","lineNumber":136,"sourceCode":"  const error: NodeJS.ErrnoException = new Error(\n    `require() cannot be used to load ES Module ${modulePath}: ${detail}`,\n  );\n  error.code = 'ERR_REQUIRE_ASYNC_MODULE';\n  return error;\n}\n\n// Decode a `data:` URI specifier into its mime type and decoded code/body.\n// `application/wasm` returns a Buffer; everything else returns a UTF-8 string.\nconst dataURIRegex =\n  /^data:(?<mime>text\\/javascript|application\\/json|application\\/wasm)(?:;(?<encoding>charset=utf-8|base64))?,(?<code>.*)$/;\n\nfunction parseDataUri(specifier: string): {\n  mime: string;\n  code: string | Buffer;\n} {\n  const match = specifier.match(dataURIRegex);\n  if (!match || !match.groups) {\n    throw new Error('Invalid data URI');\n  }\n  const {mime, encoding, code} = match.groups;\n  if (mime === 'application/wasm') {\n    if (!encoding) throw new Error('Missing data URI encoding');\n    if (encoding !== 'base64') {\n      throw new Error(`Invalid data URI encoding: ${encoding}`);\n    }\n    return {code: Buffer.from(code, 'base64'), mime};\n  }\n  if (!encoding || encoding === 'charset=utf-8') {\n    return {code: decodeURIComponent(code), mime};\n  }\n  if (encoding === 'base64') {\n    return {code: Buffer.from(code, 'base64').toString(), mime};\n  }\n  throw new Error(`Invalid data URI encoding: ${encoding}`);\n}\n","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/jestjs/jest/blob/f49721c78e195558b40913977c9230f5b7f559d8/packages/jest-runtime/src/internals/EsmLoader.ts#L118-L154","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nimport code from 'data:application/javascript,export const x = 1;';\n\n// after\nimport code from 'data:text/javascript,export%20const%20x%20%3D%201;';","handlingStrategy":"validation","validationCode":"const DATA_URI_RE = /^data:(text\\/javascript|application\\/json|application\\/wasm)(;charset=utf-8|;base64)?,(.*)$/;\nfunction isValidDataUri(uri) {\n  return DATA_URI_RE.test(uri);\n}\n// use: if (!isValidDataUri(specifier)) throw new Error('Bad data URI');","typeGuard":"function isDataUriSpecifier(s: string): boolean {\n  return /^data:(text\\/javascript|application\\/json|application\\/wasm)(;charset=utf-8|;base64)?,/.test(s);\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["esm","data-uri","imports"],"analyzedSha":"f49721c78e195558b40913977c9230f5b7f559d8","analyzedAt":"2026-08-03T20:16:28.571Z","schemaVersion":2}