I got an answer from Vite Discord channel. This is the solution to convert postcss and tailwindcss config files to ESModule.

Do this, and you can use import in those config files.

tailwind.config.js

export default {
  purge: ['./*.html', './src/**/*.{vue,js,ts,jsx,tsx,css}'],
  darkMode: false, // or 'media' or 'class'
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
}

postcss.config.js

import tailwind from 'tailwindcss'
import autoprefixer from 'autoprefixer'
import tailwindConfig from './tailwind.config.js'

export default {
  plugins: [tailwind(tailwindConfig), autoprefixer],
}

vite.config.js I added import postcss from './postcss.config.js' and

css: {
  postcss,
},
Answer from Shook Lyngs on Stack Overflow
🌐
GitHub
github.com › tailwindlabs › tailwindcss › discussions › 5858
ES Module Support for config · tailwindlabs/tailwindcss · Discussion #5858
This is a request to enable module support from tailwind. Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /var/www/craft/config/build/tailwind.config.js require() of ES modules is not supported.
Author   tailwindlabs
🌐
GitHub
github.com › tailwindlabs › tailwindcss-intellisense › issues › 592
require() of ES Module is not supported · Issue #592 · tailwindlabs/tailwindcss-intellisense
August 9, 2022 - require() of ES Module is not supported#592 · Copy link · muuvmuuv · opened · on Aug 9, 2022 · Issue body actions · What version of Tailwind CSS IntelliSense are you using? For example: v0.8.6 · What version of Tailwind CSS are you using? For example: v3.1.8 ·
Author   muuvmuuv
Discussions

Tailwind.config.js and require vs import / ESM
Has anybody figured out how to replace old require statements with ES imports? For example: /** @type {import('tailwindcss').Config} config */ // How to replace defaultTheme require? const defaultTheme = import('tailwindcss').defaultTheme const config = { theme: { extend: { fontFamily: { sans: ... More on discourse.roots.io
🌐 discourse.roots.io
2
0
August 20, 2024
How to import ES module in Tailwind config file?
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I am configuring my own plugins in Tailwind config file by import ES module with CommonJS syntax require('/my-plugin'); but I get an error when trying to build: More on stackoverflow.com
🌐 stackoverflow.com
Require() of ES Module prettier-plugin-tailwindcss/dist/index.mjs not supported
Instead change the require of prettier-plugin-tailwindcss/dist/index.mjs to a dynamic import() which is available in all CommonJS modules. I get this error as creating a new Qwik - Site with Visual CMS npm run qwik add ==> tailwind ‘npm run dev’ in my WSL2 using my browser to run click ... More on forum.builder.io
🌐 forum.builder.io
8
0
December 1, 2023
ESM Syntax causing extension to not work.
What version of Tailwind CSS IntelliSense are you using? For example: v0.8.7 What version of Tailwind CSS are you using? For example: v3.1.8 What package manager are you using? PNPM 7.12.2 What ope... More on github.com
🌐 github.com
1
October 2, 2022
🌐
GitHub
github.com › tailwindlabs › tailwindcss › discussions › 8282
Support for ES6 modules · tailwindlabs/tailwindcss · Discussion #8282
ERROR in ./src/index.css (./node_modules/css-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[5].use[1]!./node_modules/react-scripts/node_modules/postcss-loader/dist/cjs.js??ruleSet[1].rules[1].oneOf[5].use[2]!./node_modules/source-map-loader/dist/cjs.js!./src/index.css) Module build failed (from ./node_modules/react-scripts/node_modules/postcss-loader/dist/cjs.js): Error [ERR_REQUIRE_ESM]: require() of ES Module /../client/tailwind.config.js from /../client/node_modules/tailwindcss/lib/lib/setupTrackingContext.js not supported.
Author   tailwindlabs
🌐
Js
tw-themes.js.org › concepts › esModules.html
A Note on ES Modules in Tailwind Configuration
March 19, 2021 - rollup.config.js ================ import tailwindcss from 'tailwindcss'; // NEW: in support of ES Modules import tailwindConfig from './tailwind.config.js'; // (found in tailwind.config.js) ... snip snip export default { ... snip snip plugins: [ svelte({ ... snip snip preprocess: sveltePreprocess({ ... snip snip postcss: { plugins: [ ... snip snip // require("tailwindcss"), // ...
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 10972330695698-Phpstrom-tailwind-css-autocomplete-not-working-in-vite-react-project-
Phpstrom tailwind css autocomplete not working in vite react project – IDEs Support (IntelliJ Platform) | JetBrains
April 12, 2023 - Instead rename tailwind.config.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /mnt/DevOps/devops/text-to-image/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead). Error [ERR_REQUIRE_ESM]: require() of ES Module /mnt/DevOps/devops/text-to-image/tailwind.config.js from /snap/phpstorm/329/plugins/tailwindcss/server/tailwindcss-language-server not supported.
Find elsewhere
🌐
Builder.io
forum.builder.io › technical questions
Require() of ES Module prettier-plugin-tailwindcss/dist/index.mjs not supported - Technical Questions - Builder.io Forum
December 1, 2023 - Instead change the require of prettier-plugin-tailwindcss/dist/index.mjs to a dynamic import() which is available in all CommonJS modules. I get this error as creating a new Qwik - Site with Visual CMS npm run qwik add ==> tailwind ‘npm run dev’ in my WSL2 using my browser to run click ...
🌐
Liamhammett
liamhammett.com › tailwindcss-config-with-esm-imports
TailwindCSS Config with ESM Imports - Liam Hammett
Here’s how you can convert TailwindCSS’ configuration file to use ESM imports. Remember that not everyone who uses TailwindCSS is familiar with the JavaScript ecosystem, so while this might be obvious to anyone who works with NodeJS, folks working primarily in other backends might not. To start, here’s what a typical default tailwind.config.js file will look like. const plugin = require('tailwindcss/plugin') module.exports = { content: [ "./resources/**/*.blade.php", "./resources/**/*.js", "./resources/**/*.vue", ], plugins: [ require("@tailwindcss/typography"), require("@tailwindcss/forms"), ], };
🌐
GitHub
github.com › tailwindlabs › tailwindcss-intellisense › issues › 618
ESM Syntax causing extension to not work. · Issue #618 · tailwindlabs/tailwindcss-intellisense
October 2, 2022 - Instead change the require of tailwind.config.js in c:\Users\radd8\.vscode\extensions\bradlc.vscode-tailwindcss-0.8.7\dist\tailwindServer.js to a dynamic import() which is available in all CommonJS modules. Error [ERR_REQUIRE_ESM]: require() of ES Module e:\Programming\musicChartSite\tailwind.config.js from c:\Users\radd8\.vscode\extensions\bradlc.vscode-tailwindcss-0.8.7\dist\tailwindServer.js not supported.
Author   buffet-time
🌐
GitHub
github.com › tailwindlabs › prettier-plugin-tailwindcss › issues › 196
Error Require of ES Module not supported in v0.5.0 · Issue #196 · tailwindlabs/prettier-plugin-tailwindcss
August 10, 2023 - [error] require() of ES Module ...path\node_modules\prettier-plugin-tailwindcss\dist\index.mjs not supported.
Author   codeflorist
🌐
Answer Overflow
answeroverflow.com › m › 1246312315146604575
Tailwind CSS: Unhandled exception: require() of ES Module - Nuxt
June 1, 2024 - How to fix this error I keep getting ... Instead change the require of D:\WebDev\nuxtApp\node_modules\@nuxtjs\tailwindcss\dist\runtime\merger.mjs to a dynamic import() which is available in all CommonJS modules....
🌐
GitHub
github.com › tailwindlabs › tailwindcss › discussions › 2284
TailwindCSS fails in an esm project · tailwindlabs/tailwindcss · Discussion #2284
I'd be extremely grateful if tailwind could introduce some kind of detection to tell esm and commonjs tailwind.config.js files apart, or just mandate the use of esm syntax and the .mjs file extension for the tailwind config file as the default. ES Modules are the only true standard-conformant way of using modules, and even though they're in experimental support, they're also a lot nicer to work with and a migration to esm now would definitely prepare tailwindcss for the future.
Author   tailwindlabs
🌐
GitHub
yellowduck.be › posts › til-fixing-the-require-error-in-tailwind-config-js-after-updating-nodejs-to-version-23
🐥 TIL: Fixing the require error in tailwind.config.js after updating NodeJS to version 23
November 20, 2024 - plugins: [require('@tailwindcss/container-queries')], ... After some searching around on the web, this is caused by the fact that Node.js 23 unflagged --experimental-require-module which causes the warning when ESM config files are loaded.
🌐
Tailwind CSS
v3.tailwindcss.com › docs › configuration
Configuration - Tailwind CSS
/** @type {import('tailwindcss').Config} */ module.exports = { // ... presets: [ require('@acmecorp/base-tailwind-config') ], // Project-specific customizations theme: { //... }, } Learn more about presets in the presets documentation. The prefix option allows you to add a custom prefix to all of Tailwind’s generated utility classes. This can be really useful when layering Tailwind on top of existing CSS where there might be naming conflicts.
🌐
Tailwind CSS
tailwindcss.com › docs › plugins
Plugins - Tailwind CSS
March 28, 2023 - Plugins let you register new styles for Tailwind to inject into the user’s stylesheet using JavaScript instead of CSS. To get started with your first plugin, import Tailwind’s plugin function from tailwindcss/plugin. Then inside your plugins array, call the imported plugin function with an anonymous function as the first argument. ... const plugin = require('tailwindcss/plugin') module.exports = { plugins: [ plugin(function({ addUtilities, addComponents, e, config }) { // Add your custom styles here }), ] }
🌐
Tailwind CSS
v3.tailwindcss.com › docs › using-with-preprocessors
Using with Preprocessors - Tailwind CSS
@tailwind base; @import "./custom-base-styles.css"; @tailwind components; @import "./custom-components.css"; @tailwind utilities; @import "./custom-utilities.css"; You can solve this by creating separate files for each @tailwind declaration, and then importing those files in your main stylesheet. To make this easy, we provide separate files for each @tailwind declaration out of the box that you can import directly from node_modules.