npm run dev stops with an internal PostCSS error
[vite] Internal server error: Failed to load PostCSS configShort answer
Vite loads postcss.config.js through postcss-load-config. It fails when the file uses module.exports while package.json says type module, or when a plugin named in the file is not installed. Rename the file to .cjs or use export default, and run npm install for the plugin.
Fix it fast
1Find out which cause you have
Open the full first error in the terminal, not just the headline. The line under it names the real problem. Also see the module type of the project.
grep '"type"' package.json ls postcss.config.*2Match what you see, then jump to its fix
What does the error look like?
[vite] Internal server error: Failed to load PostCSS configFailed to load PostCSS config
Cause 1 of 2
CommonJS config in a project marked as ES module
1Check
Node.js loads .js files as ES modules when the nearest package.json has type module, and always loads .cjs as CommonJS. A config written with module.exports then breaks.
grep '"type"' package.json
head -5 postcss.config.js2Fix
Either rename the file to postcss.config.cjs, or rewrite it as an ES module.
// postcss.config.js (ES module form)
export default {
plugins: {
autoprefixer: {},
},
};3Verify
Restart the dev server.
npm run devConfirmed by nodejs.org
Cause 2 of 2
A plugin named in the config is not installed
1Check
In the object form of the config, each key is a Node.js module name that must resolve from the project. Compare the keys with node_modules.
cat postcss.config.js
npm ls autoprefixer postcss2Fix
Install the missing packages as dev dependencies, then run again.
npm install -D postcss autoprefixer3Verify
The dev server starts without the error.
npm run devConfirmed by github.com
How do I stop it coming back?
- Pick one module format for config files and keep to it, or name them .cjs / .mjs explicitly.
- After pulling changes, run npm install before npm run dev.
- Keep only one PostCSS config file in the project root.
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