Maaz Zindani

Loading the index…

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

npm run dev stops with an internal PostCSS error

Error[vite] Internal server error: Failed to load PostCSS config

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

  • Applies to Vite, postcss-load-config, Node.js 22, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.*
  2. 2Match what you see, then jump to its fix

    1. 1An error saying module or require is not defined, raised while loading postcss.config.jsCommonJS config in a project marked as ES moduleFix
    2. 2An error that names a plugin package (for example autoprefixer) it cannot findA plugin named in the config is not installedFix

What does the error look like?

  • [vite] Internal server error: Failed to load PostCSS config
  • Failed 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.js

2Fix

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 dev

Confirmed by nodejs.org

Did this fix it?

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 postcss

2Fix

Install the missing packages as dev dependencies, then run again.

npm install -D postcss autoprefixer

3Verify

The dev server starts without the error.

npm run dev

Confirmed by github.com

Did this fix it?

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 fix

Next step

Want this handled for good?

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