Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Vite cannot read your vite.config file

Errorfailed to load config from /path/to/project/vite.config.js

Short answer

Vite loads vite.config as a file in its own right, and it must be detected as ESM by Node.js if it uses import and export. The error is usually that mismatch: the file uses ESM syntax but is treated as CommonJS, or it requires an ESM-only package. A folder name with an ampersand also breaks npm.

  • Applies to Vite 8, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the full message under the first line. It names the real error (module syntax, ESM or module not found).

    npx vite
  2. 2Match what you see, then jump to its fix

    1. 1The message under the first line complains about import or export syntax in vite.config.jsThe config uses import/export but is not detected as ESMFix
    2. 2The message says an ES module cannot be loaded with require, and the config pulls in a pluginThe config requires an ESM-only packageFix
    3. 3Cannot find module, with a folder name that has & in itThe project path contains an ampersand and you use npmFix

What does the error look like?

  • failed to load config from /path/to/project/vite.config.js
  • Cannot find module

Cause 1 of 3

The config uses import/export but is not detected as ESM

1Check

To use ES module syntax, the config must be in a file Node.js detects as ESM: a .mjs file, or .js with type module in the closest package.json.

grep -n '"type"' package.json

2Fix

Add type module to package.json, or rename the config to vite.config.mjs (or .mts for TypeScript).

{
  "type": "module"
}

3Verify

Start Vite again; the config is read without the load error.

npx vite

Confirmed by vite.dev

Did this fix it?

Cause 2 of 3

The config requires an ESM-only package

1Check

Node.js 22 and earlier cannot require() an ES module. A CommonJS config that requires such a plugin fails.

node --version && grep -n 'require(' vite.config.*

2Fix

Make the config ESM with type module in the nearest package.json, or use the .mjs or .mts extension.

// vite.config.mjs
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
export default defineConfig({ plugins: [react()] })

3Verify

Run the dev server.

npx vite

Confirmed by vite.dev

Did this fix it?

Cause 3 of 3

The project path contains an ampersand and you use npm

1Check

npm fails when the project path contains &. Look at the full path of the project.

pwd

2Fix

Use pnpm or yarn, or move the project to a path without the ampersand.

pnpm install
pnpm exec vite

3Verify

Start the dev server from the same folder.

pnpm exec vite

Confirmed by vite.dev

Did this fix it?

How do I stop it coming back?

  • Use type module in package.json for every Vite project so the config and the app agree.
  • Keep project folder names free of & and other shell-special characters.
  • Write the config in ESM syntax only; do not mix require and import.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.