vitejs/vite · error · Error

import.meta.resolve is not supported in CJS config files

Error message

import.meta.resolve is not supported in CJS config files

What it means

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.

Solutions

  1. Switch the config to ESM (vite.config.mjs or package.json type: module) so import.meta.resolve works.
  2. Replace import.meta.resolve with require.resolve (CJS) or path.resolve relative to __dirname.
  3. Move the resolution logic into a small ESM helper module imported by the config.

Example fix

// before (vite.config.cjs)
const resolved = import.meta.resolve('./plugin.js')
// after
const resolved = require.resolve('./plugin.js')
Defensive patterns

Strategy: validation

Validate before calling

function configIsCommonjs(filename) {
  return filename.endsWith('.cjs') || (filename.endsWith('.js') && packageJsonType(filename) !== 'module');
}

Type guard

function supportsImportMetaResolve(configFilename, pkgType) {
  return configFilename.endsWith('.mjs') || pkgType === 'module' || configFilename.endsWith('.mts');
}

Prevention

When it happens

Trigger: Calling import.meta.resolve(specifier) inside a vite.config.cjs (or a .js config resolved as CJS via package.json type: commonjs).

Common situations: 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.

Related errors


AI-assisted analysis of vitejs/vite@b4d66fee14 (2026-08-11). Data as JSON: /api/errors/305a624024ff5c73. Report an issue: GitHub.

Appendix: source

Thrown at packages/vite/src/node/config.ts:2626

        name: 'inject-file-scope-variables',
        transform: {
          filter: { id: /\.[cm]?[jt]s$/ },
          handler(code, id) {
            let injectValues =
              `const ${dirnameVarName} = ${JSON.stringify(path.dirname(id))};` +
              `const ${filenameVarName} = ${JSON.stringify(id)};` +
              `const ${importMetaUrlVarName} = ${JSON.stringify(
                pathToFileURL(id).href,
              )};`
            if (importMetaResolveRegex.test(code)) {
              if (isESM) {
                if (!importMetaResolverRegistered) {
                  importMetaResolverRegistered = true
                  createImportMetaResolver()
                }
                injectValues += `const ${importMetaResolveVarName} = (specifier, importer = ${importMetaUrlVarName}) => (${importMetaResolveWithCustomHookString})(specifier, importer);`
              } else {
                injectValues += `const ${importMetaResolveVarName} = (specifier, importer = ${importMetaUrlVarName}) => { throw new Error('import.meta.resolve is not supported in CJS config files') };`
              }
            }

            let injectedContents: string
            if (code.startsWith('#!')) {
              const fileStartIndex = getFileStartIndex(code)
              const hashbang = code.slice(0, fileStartIndex)
              injectedContents =
                hashbang +
                (lineTerminatorRE.test(hashbang) ? '' : '\n') +
                injectValues +
                code.slice(fileStartIndex)
            } else {
              injectedContents = injectValues + code
            }

            return {
              code: injectedContents,

View on GitHub (pinned to b4d66fee14)