{"id":"305a624024ff5c73","repo":"vitejs/vite","slug":"import-meta-resolve-is-not-supported-in-cjs-config","errorCode":null,"errorMessage":"import.meta.resolve is not supported in CJS config files","messagePattern":"import\\.meta\\.resolve is not supported in CJS config files","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vite/src/node/config.ts","lineNumber":2626,"sourceCode":"        name: 'inject-file-scope-variables',\n        transform: {\n          filter: { id: /\\.[cm]?[jt]s$/ },\n          handler(code, id) {\n            let injectValues =\n              `const ${dirnameVarName} = ${JSON.stringify(path.dirname(id))};` +\n              `const ${filenameVarName} = ${JSON.stringify(id)};` +\n              `const ${importMetaUrlVarName} = ${JSON.stringify(\n                pathToFileURL(id).href,\n              )};`\n            if (importMetaResolveRegex.test(code)) {\n              if (isESM) {\n                if (!importMetaResolverRegistered) {\n                  importMetaResolverRegistered = true\n                  createImportMetaResolver()\n                }\n                injectValues += `const ${importMetaResolveVarName} = (specifier, importer = ${importMetaUrlVarName}) => (${importMetaResolveWithCustomHookString})(specifier, importer);`\n              } else {\n                injectValues += `const ${importMetaResolveVarName} = (specifier, importer = ${importMetaUrlVarName}) => { throw new Error('import.meta.resolve is not supported in CJS config files') };`\n              }\n            }\n\n            let injectedContents: string\n            if (code.startsWith('#!')) {\n              const fileStartIndex = getFileStartIndex(code)\n              const hashbang = code.slice(0, fileStartIndex)\n              injectedContents =\n                hashbang +\n                (lineTerminatorRE.test(hashbang) ? '' : '\\n') +\n                injectValues +\n                code.slice(fileStartIndex)\n            } else {\n              injectedContents = injectValues + code\n            }\n\n            return {\n              code: injectedContents,","sourceCodeStart":2608,"sourceCodeEnd":2644,"githubUrl":"https://github.com/vitejs/vite/blob/89620f09afcfef6b35e7bb8660132ab5b4d0cd3b/packages/vite/src/node/config.ts#L2608-L2644","documentation":"Injected into the bundled config source when the config file is CommonJS but its code references `import.meta.resolve`. Vite shims `import.meta` variables in CJS configs, and since `import.meta.resolve` is an ESM-only runtime feature with no CJS equivalent, it injects a thunk that throws this message at call time. It is produced by the `inject-file-scope-variables` transform (config.ts:2626) only on the `!isESM` branch.","triggerScenarios":"Authoring a `vite.config.cjs` (or any config bundled to CJS) that calls `import.meta.resolve(...)`. At runtime, when that call executes, the injected stub throws.","commonSituations":"Copy-pasting config snippets from ESM examples into a CJS config; using `import.meta.resolve` to locate a path in a project that hasn't enabled `\"type\": \"module\"`; migrating partially to ESM tooling.","solutions":["Convert the config to ESM (rename to `vite.config.mjs` or set `\"type\": \"module\"`) so the real `import.meta.resolve` shim is wired up.","Replace `import.meta.resolve(spec)` with `require.resolve(spec)` (and a `pathToFileURL`/`fileURLToPath` dance) while staying in CJS.","Use `createRequire(import.meta.url).resolve(...)` only after moving to ESM — it is not available in pure CJS either."],"exampleFix":"// before (vite.config.cjs)\nconst resolved = import.meta.resolve('./src/entry') // throws\n\n// after (vite.config.mjs)\nconst resolved = import.meta.resolve('./src/entry')","handlingStrategy":"validation","validationCode":"// Reject import.meta.resolve usage in CJS configs before bundling\nconst isCjs = configPath.endsWith('.cjs') || (configPath.endsWith('.js') && pkg.type !== 'module')\nif (isCjs && /import\\.meta\\.resolve/.test(fs.readFileSync(configPath, 'utf8'))) {\n  throw new Error('import.meta.resolve is unavailable in CJS configs; use .mjs')\n}","typeGuard":"function isEsmConfig(p: string): boolean {\n  return p.endsWith('.mjs') || (p.endsWith('.js') && require('./package.json').type === 'module')\n}","tryCatchPattern":null,"preventionTips":["Keep all Vite configs in ESM (.mjs or \"type\": \"module\").","Use require.resolve() as the CJS equivalent when you must stay CJS."],"tags":["esm","cjs","config","import-meta"],"analyzedSha":"89620f09afcfef6b35e7bb8660132ab5b4d0cd3b","analyzedAt":"2026-08-03T19:28:02.920Z","schemaVersion":2}