{"record":{"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/b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab/packages/vite/src/node/config.ts#L2608-L2644","documentation":"When the config bundler injects file-scope variables, it rewrites import.meta.resolve to a helper. In ESM configs that helper resolves specifiers; in CJS configs there is no import.meta, so Vite injects a stub that throws 'import.meta.resolve is not supported in CJS config files' at call time. Using import.meta.resolve inside a .cjs config therefore fails on first invocation.","triggerScenarios":"Calling import.meta.resolve(specifier) inside a vite.config.cjs (or a .js config resolved as CJS via package.json type: commonjs).","commonSituations":"Configs that try to resolve sibling modules by absolute path; copy-pasting ESM-style config snippets into a CommonJS project; Windows setups where path resolution helpers are needed.","solutions":["Switch the config to ESM (vite.config.mjs or package.json type: module) so import.meta.resolve works.","Replace import.meta.resolve with require.resolve (CJS) or path.resolve relative to __dirname.","Move the resolution logic into a small ESM helper module imported by the config."],"exampleFix":"// before (vite.config.cjs)\nconst resolved = import.meta.resolve('./plugin.js')\n// after\nconst resolved = require.resolve('./plugin.js')","handlingStrategy":"validation","validationCode":"function configIsCommonjs(filename) {\n  return filename.endsWith('.cjs') || (filename.endsWith('.js') && packageJsonType(filename) !== 'module');\n}","typeGuard":"function supportsImportMetaResolve(configFilename, pkgType) {\n  return configFilename.endsWith('.mjs') || pkgType === 'module' || configFilename.endsWith('.mts');\n}","tryCatchPattern":null,"preventionTips":["Avoid import.meta.resolve in CJS configs; use require.resolve.","Convert the config to ESM if you rely on import.meta."],"tags":["config","esm","commonjs","import-meta"],"backgroundTag":null,"analyzedSha":"b4d66fee14d970f45b8a6f3d7d6aee73ca9b88ab","analyzedAt":"2026-08-11T11:49:19.515Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}