Vite cannot read postcss.config.js because the project is an ES module
Your error
Failed to load PostCSS config (searchPath: /app): [ReferenceError] module is not defined in ES module scope2 more lines it can showFix it fast
Most common cause, 1 of 3The config uses module.exports in a "type": "module" package
Switch the file to ES module syntax.
// postcss.config.js
export default {
plugins: {
autoprefixer: {},
},
}Short answer
Your package.json has "type": "module", so Node.js loads every .js file, postcss.config.js included, as an ES module. ES modules have no module.exports or require, so loading the config throws and Vite stops. Either rewrite the file with export default and import, or keep the CommonJS code and rename the file to postcss.config.cjs.
Which cause do you have?
Compare the module type of the package with the syntax inside the config file. A mismatch between the two is the whole problem.
node -p "require('./package.json').type"
head -3 postcss.config.*Then find the line you see in this list and open its fix:
- 1The config uses module.exports in a "type": "module" package
ReferenceError: module is not defined in ES module scope, with the hint to rename the file to .cjs - 2You want to keep the CommonJS syntax
Several tools in the repo share the file, or you prefer not to touch its contents - 3The config was half converted and still calls require()
The message changes to require is not defined in ES module scope, you can use import instead
What does the error look like?
Failed to load PostCSS config (searchPath: /app): [ReferenceError] module is not defined in ES module scopeThis file is being treated as an ES module because it has a '.js' file extension and '/app/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.Failed to load PostCSS config: [ReferenceError] require is not defined in ES module scope, you can use import instead
Cause 1 of 3
The config uses module.exports in a "type": "module" package
You seeReferenceError: module is not defined in ES module scope, with the hint to rename the file to .cjs
Check
Node.js decides the format of a .js file from the nearest package.json. With type set to module the file is ESM, and ESM has no module object. Templates generated for CommonJS projects often still write module.exports in this file.
grep -n 'module.exports' postcss.config.js tailwind.config.js 2>/dev/nullFix
Switch the file to ES module syntax. The plugin object form needs no imports at all.
// postcss.config.js
export default {
plugins: {
autoprefixer: {},
},
}Verify
Run the build. Vite loads the config and the CSS is processed.
npx vite buildConfirmed by nodejs.org
Cause 2 of 3
You want to keep the CommonJS syntax
You seeSeveral tools in the repo share the file, or you prefer not to touch its contents
Check
A .cjs extension always means CommonJS, whatever the type field says. Vite accepts any config format that postcss-load-config supports, and that includes postcss.config.cjs.
ls postcss.config.*Fix
Rename the file and leave the code as it is. Make sure only one PostCSS config file remains in the folder.
git mv postcss.config.js postcss.config.cjsVerify
The build succeeds with the renamed file.
npx vite buildConfirmed by vite.dev
Cause 3 of 3
The config was half converted and still calls require()
You seeThe message changes to require is not defined in ES module scope, you can use import instead
Check
Changing module.exports to export default is not enough when the plugins are loaded with require('autoprefixer'). The Node.js docs list require as one of the things ES modules do not have.
grep -n 'require(' postcss.config.jsFix
Import each plugin at the top and list the imported functions.
// postcss.config.js
import autoprefixer from 'autoprefixer'
export default {
plugins: [autoprefixer],
}Verify
The build passes and the output CSS still has the vendor prefixes or transforms the plugins add.
npx vite build && ls dist/assets/*.cssConfirmed by nodejs.org
How do I stop it coming back?
- When you add "type": "module", check every *.config.js in the root for module.exports or require.
- Use .cjs or .mjs on config files when a repo mixes formats, so the extension states the format.
- Keep one PostCSS config per project; several formats side by side make it unclear which one loads.
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: