Maaz Zindani

Loading the index…

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

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 show

Fix it fast

Most common cause, 1 of 3

The config uses module.exports in a "type": "module" package

Switch the file to ES module syntax.

Fix
// postcss.config.js
export default {
  plugins: {
    autoprefixer: {},
  },
}
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to Vite 8.3, Node.js 24.21, autoprefixer 10
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
node -p "require('./package.json').type"
head -3 postcss.config.*

Then find the line you see in this list and open its fix:

  1. 1The config uses module.exports in a "type": "module" packageReferenceError: module is not defined in ES module scope, with the hint to rename the file to .cjs
  2. 2You want to keep the CommonJS syntaxSeveral tools in the repo share the file, or you prefer not to touch its contents
  3. 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 scope
  • This 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.

Check
grep -n 'module.exports' postcss.config.js tailwind.config.js 2>/dev/null

Fix

Switch the file to ES module syntax. The plugin object form needs no imports at all.

Fix
// postcss.config.js
export default {
  plugins: {
    autoprefixer: {},
  },
}

Verify

Run the build. Vite loads the config and the CSS is processed.

Verify
npx vite build

Confirmed by nodejs.org

Did this fix it?

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.

Check
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.

Fix
git mv postcss.config.js postcss.config.cjs

Verify

The build succeeds with the renamed file.

Verify
npx vite build

Confirmed by vite.dev

Did this fix it?

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.

Check
grep -n 'require(' postcss.config.js

Fix

Import each plugin at the top and list the imported functions.

Fix
// 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.

Verify
npx vite build && ls dist/assets/*.css

Confirmed by nodejs.org

Did this fix it?

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:

Next step

Want this handled for good?

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