Vite cannot read your vite.config file
failed to load config from /path/to/project/vite.config.jsShort 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.
Fix it fast
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 vite2Match what you see, then jump to its fix
- 1
The 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
The message says an ES module cannot be loaded with require, and the config pulls in a pluginThe config requires an ESM-only packageFix - 3
Cannot find module, with a folder name that has & in itThe project path contains an ampersand and you use npmFix
- 1
What does the error look like?
failed to load config from /path/to/project/vite.config.jsCannot 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.json2Fix
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 viteConfirmed by vite.dev
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 viteConfirmed by vite.dev
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.
pwd2Fix
Use pnpm or yarn, or move the project to a path without the ampersand.
pnpm install
pnpm exec vite3Verify
Start the dev server from the same folder.
pnpm exec viteConfirmed by vite.dev
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 fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix