cypress-io/cypress · error · CJSNotSupportedError

CJS builds of vite ${majorVersionNumber} are not supported

Error message

CJS builds of vite ${majorVersionNumber} are not supported

What it means

Thrown as CJSNotSupportedError by getVite when the ESM import of Vite fails AND the detected Vite major is >= 7. Vite 7 removed its CommonJS build entirely, so the previous fallback (import the CJS build, used for Vite 4–6) no longer exists. The error signals that the only supported Vite build (ESM) could not be imported and there is no CJS alternative to retry.

Source

Thrown at npm/vite-dev-server/src/getVite.ts:72

      esmPath = viteExports.import
      cjsPath = viteExports.require
    }

    debug('vite ESM build path: %s', esmPath)
    debug('vite CJS build path: %s', cjsPath)

    try {
      // try to import the ESM build of Vite
      const esmViteImportPath = path.resolve(vitePackageJsonPath, '../', esmPath)

      debug('resolved esmViteImportPath as %s', esmViteImportPath)

      const viteImport = await import(`${filePrefix}${esmViteImportPath}`)

      return viteImport
    } catch (err) {
      if (majorVersionNumber >= 7) {
        throw new CJSNotSupportedError(`CJS builds of vite ${majorVersionNumber} are not supported`)
      }

      // if the ESM build import fails, try to import the CJS build
      debug('importing vite as ESM failed:', err)
      debug('importing vite as CJS')
      // Vite 4-6 both include the CJS distribution of Vite
      const cjsViteImportPath = path.resolve(vitePackageJsonPath, '../', cjsPath)

      debug('resolved cjsViteImportPath as %s', cjsViteImportPath)

      const viteImport = await import(`${filePrefix}${cjsViteImportPath}`)

      return viteImport.default
    }
  } catch (err) {
    if (err instanceof CJSNotSupportedError) {
      throw err
    }

View on GitHub (pinned to 0d85fdc912)

Solutions

  1. Use Node 20.19+ or 22+ (Vite 7's minimum) and ensure the project's package.json is not forcing CommonJS where Vite's ESM must load.
  2. Reinstall Vite cleanly: `npm install vite@7` to restore a complete ESM build.
  3. Remove any ts-node/babel rewrites that transform dynamic `import()` into require() for the Vite path.
  4. If your toolchain cannot load ESM, stay on Vite 6 which still ships a CJS fallback.

Example fix

// before: project forces CJS, breaks vite 7 ESM
// package.json
{ "type": "commonjs" }
// after
{ "type": "module" }  // or downgrade to vite@6
Defensive patterns

Strategy: validation

Validate before calling

import gte from 'semver/functions/gte.js'
import { createRequire } from 'module'
const r = createRequire(import.meta.url)
const v = r('vite/package.json').version
if (gte(v, '7.0.0') && process.version.startsWith('v18')) throw new Error('Node 20+ required for Vite 7')

Type guard

const supportsVite7Esm = (): boolean => Number(process.versions.node.split('.')[0]) >= 20

Try / catch

try { vite = await getVite(config) } catch (e) { if (e instanceof CJSNotSupportedError || /CJS builds of vite/.test(e.message)) { /* use vite@6 fallback */ } else throw e }

Prevention

When it happens

Trigger: getVite resolves vite/package.json, finds majorVersionNumber >= 7, attempts `import(esmViteImportPath)` and it throws — then rethrows as CJSNotSupportedError. Typical causes: the ESM entry path is wrong/corrupted, the Vite install is incomplete, a transpiler/runner strips dynamic ESM import, or the project's Node version cannot load Vite 7's ESM.

Common situations: Upgrading to Vite 7 in an environment that mangles ESM (older Jest config, ts-node with CommonJS module setting, a bundler that rewrites import()); a partial/corrupted vite install; running under a Node version below Vite 7's requirement.

Related errors


AI-assisted analysis of cypress-io/cypress@0d85fdc912 (2026-08-12). Data as JSON: /api/errors/e7abade38edc33f5. Report an issue: GitHub.