{"record":{"id":"fe8b95a042e33c2f","repo":"facebook/react","slug":"expected-source-to-have-been-loaded-into-a-string","errorCode":null,"errorMessage":"Expected source to have been loaded into a string.","messagePattern":"Expected source to have been loaded into a string\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/react-server-dom-esm/src/ReactFlightESMNodeLoader.js","lineNumber":794,"sourceCode":"      context.url,\n      (url: string, ctx: LoadContext, defaultLoad: LoadFunction) => {\n        return loadClientImport(url, defaultTransformSource);\n      },\n    );\n    return {source: newSrc};\n  }\n  return transformed;\n}\n\nexport async function load(\n  url: string,\n  context: LoadContext,\n  defaultLoad: LoadFunction,\n): Promise<{format: string, shortCircuit?: boolean, source: Source}> {\n  const result = await defaultLoad(url, context, defaultLoad);\n  if (result.format === 'module') {\n    if (typeof result.source !== 'string') {\n      throw new Error('Expected source to have been loaded into a string.');\n    }\n    const newSrc = await transformModuleIfNeeded(\n      result.source,\n      url,\n      defaultLoad,\n    );\n    return {format: 'module', source: newSrc};\n  }\n  return result;\n}\n","sourceCodeStart":776,"sourceCodeEnd":805,"githubUrl":"https://github.com/facebook/react/blob/eafeac097ba51e1eab809c07102126bd5f8e5425/packages/react-server-dom-esm/src/ReactFlightESMNodeLoader.js#L776-L805","documentation":"The loader's load hook requires that defaultLoad's result for format 'module' carry a string source, because transformModuleIfNeeded then parses the source for 'use client'/'use server' directives and rewrites client-reference imports. Node legally hands loaders ArrayBuffer or Uint8Array sources, and any non-string reaching this check throws.","triggerScenarios":"defaultLoad (or an earlier loader in the chain) returns {format: 'module', source: ArrayBuffer|Uint8Array|Buffer} — for example a chained loader returning Buffers, or a URL scheme that Node loads as binary while still reporting format 'module'.","commonSituations":"Chained loaders (asset/WASM/CSS loaders returning buffers); --experimental-network-imports; Node upgrades that change the default source representation for some schemes.","solutions":["Fix the earlier loader so JS module sources are returned as utf-8 strings before React's load hook sees them","Add an adapter load hook that converts binary module sources with new TextDecoder().decode(source)","Avoid importing modules from schemes that produce binary sources"],"exampleFix":"// before — chained loader returns a Buffer\nexport async function load(url, context, dflt) {\n  const r = await dflt(url, context, dflt);\n  return r; // r.source is a Buffer for .js files\n}\n\n// after — decode module sources to strings\nexport async function load(url, context, dflt) {\n  const r = await dflt(url, context, dflt);\n  if (r.format === 'module' && typeof r.source !== 'string') {\n    return {...r, source: new TextDecoder().decode(r.source)};\n  }\n  return r;\n}","handlingStrategy":"validation","validationCode":"// adapter load hook placed in front of React's loader\nexport async function load(url, context, dflt) {\n  const r = await dflt(url, context, dflt);\n  if (r.format === 'module' && typeof r.source !== 'string') {\n    return {...r, source: new TextDecoder().decode(r.source)};\n  }\n  return r;\n}","typeGuard":"export function isStringModuleSource(result) {\n  return result.format !== 'module' || typeof result.source === 'string';\n}","tryCatchPattern":"try {\n  await import(url);\n} catch (e) {\n  if (/loaded into a string/.test(e.message)) {\n    console.error('Binary module source for', url, '— decode it in a preceding load hook');\n  }\n  throw e;\n}","preventionTips":["Return strings for JS module sources from every custom load hook","Test the full loader chain against a 'use client' module in CI","Watch Node release notes for changes to default source types"],"tags":["node-esm","module-loader","buffer","react-server-components"],"backgroundTag":"esm-loader-source-not-string","analyzedSha":"eafeac097ba51e1eab809c07102126bd5f8e5425","analyzedAt":"2026-08-21T22:01:08.818Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}