vitejs/vite · critical · Error

Could not find original file for ${prefix}${index} in ${chun

Error message

Could not find original file for ${prefix}${index} in ${chunk.fileName}

What it means

Internal assertion in buildTimeImportMetaUrlPlugin's renderChunk hook. During the transform phase, the plugin replaces each import.meta.url usage with a placeholder token __vite_buildTimeImportMetaUrl_N and records the source file path in an idMap keyed by index. In renderChunk, it looks up the original file by index. If the index is not found in idMap, the placeholder token exists in the output but no corresponding source file was recorded — indicating state was lost or the token was introduced by something other than this plugin's transform.

Source

Thrown at packages/vite/rolldown.config.ts:360

            // import.meta.url
            s.overwrite(ss, se + 4, `${prefix}${index}`)
          }
        }
        return s.hasChanged() ? s.toString() : undefined
      },
    },
    renderChunk(code, chunk, outputOptions) {
      if (!code.includes(prefix)) return

      return code.replace(
        /__vite_buildTimeImportMetaUrl_(\d+)/g,
        (_, index) => {
          const originalFile = Object.keys(idMap).find(
            (key) => idMap[key] === +index,
          )
          if (!originalFile) {
            throw new Error(
              `Could not find original file for ${prefix}${index} in ${chunk.fileName}`,
            )
          }
          const outputFile = path.resolve(outputOptions.dir!, chunk.fileName)
          const relativePath = path
            .relative(path.dirname(outputFile), originalFile)
            .replaceAll('\\', '/')

          if (outputOptions.format === 'es') {
            return `new URL(${JSON.stringify(relativePath)}, import.meta.url)`
          } else if (outputOptions.format === 'cjs') {
            return `new URL(${JSON.stringify(
              relativePath,
            )}, require('node:url').pathToFileURL(__filename))`
          } else {
            throw new Error(`Unsupported output format ${outputOptions.format}`)
          }
        },

View on GitHub (pinned to 89620f09af)

Solutions

  1. Ensure no other plugin emits strings containing the prefix __vite_buildTimeImportMetaUrl_.
  2. Verify that the same plugin instance handles both transform and renderChunk for a given build (not recreated between phases).
  3. If modifying Vite's build config, check that the plugin's closure state persists across the build lifecycle.
Defensive patterns

Strategy: try-catch

Try / catch

// Internal build error — wrap and report
try {
  await execBuild()
} catch (e) {
  if (e.message.includes('Could not find original file for __vite_buildTimeImportMetaUrl')) {
    console.error('Build pipeline state error in import.meta.url replacement.')
    console.error('Check for plugins that duplicate or interfere with the prefix token.')
  }
  throw e
}

Prevention

When it happens

Trigger: A plugin running after buildTimeImportMetaUrlPlugin's transform but before its renderChunk introduces or duplicates the __vite_buildTimeImportMetaUrl_ prefix string. Or the module evaluator runs in a context where the transform hook's closure (idMap) is reset between transform and renderChunk (e.g., separate plugin instances).

Common situations: Contributing to Vite's codebase and modifying the build pipeline. Encountered when a new plugin or build step introduces string content matching the internal prefix pattern. Not encountered by end users of Vite.

Related errors


AI-assisted analysis of vitejs/vite@89620f09af (2026-08-03). Data as JSON: /data/errors/375ae798f17ccd3f.json. Report an issue: GitHub.