next.js: `next/lib/find-config` does not support ESM config files

Verify canary release

  • I verified that the issue exists in Next.js canary release

Provide environment information

Operating System:
      Platform: linux
      Arch: x64
      Version: #110-Ubuntu SMP PREEMPT Thu Jan 13 19:01:34 UTC 2022
    Binaries:
      Node: 16.10.0
      npm: 7.24.0
      Yarn: 1.22.17
      pnpm: N/A
    Relevant packages:
      next: 12.0.11-canary.19
      react: 17.0.2
      react-dom: 17.0.2

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

Converting Next project to "type": "module", uses Tailwind and PostCSS. next/lib/find-config does not attempt to load via dynamic import and so throws even after converting PostCSS config to ESM:

./node_modules/next/dist/build/webpack/loaders/css-loader/src/index.js??ruleSet[1].rules[2].oneOf[8].use[1]!./node_modules/next/dist/build/webpack/loaders/postcss-loader/src/index.js??ruleSet[1].rules[2].oneOf[8].use[2]!./src/styles/index.css
Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/my/repo/components/postcss.config.js from /path/to/my/repo/components/node_modules/next/dist/lib/find-config.js not supported.
Instead change the require of postcss.config.js in /path/to/my/repo/components/node_modules/next/dist/lib/find-config.js to a dynamic import() which is available in all CommonJS modules.

Expected Behavior

Should attempt to load configs via ESM-CJS-interoperable default import.

To Reproduce

Convert a Tailwind Next.js project to "type": "module" and run next dev.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 33
  • Comments: 28 (15 by maintainers)

Commits related to this issue

Most upvoted comments

If anyone is struggling with TailwindCSS + “type: module”:

  • Rename the tailwind config file to “tailwind.config.cjs”
  • Put your config from postcss.config.js as JSON to your package.json: under the key “postcss”.
  • Remove your postcss.config.js file.
  • Remove the .next folder from your project directory to clear the cache and restart

If anyone is struggling with TailwindCSS + “type: module”: 1.Rename the tailwind config file to “tailwind.config.cjs” 2.Put your config from postcss.config.js as JSON to your package.json: under the key “postcss”. 3.Remove your postcss.config.js file. 4.Remove the .next folder from your project directory to clear the cache and restart

@BB-19, No need to rename tailwind.config.js to tailwind.config.cjs. The problem is postcss.config.js, remove it and add the config to package.json. For example

{
...
  "type": "module",
  "postcss": {
    "plugins": {
      "tailwindcss": {},
      "autoprefixer": {}
    }
  },
...
}

For me, Yarn Berry with PnP Strict, the solution for convert next project from CommonJS to ESModule:

  1. Add or update package.json with “type”: “module”
  2. Remove postcss.config.js and apply it to package.json with “postcss” json format, see above example
  3. Update next.config.js and tailwind.config.js to ESM format
  4. Add “pnpEnableEsmLoader: true” to .yarnrc.yml
  5. Clear Cache yarn cache clean && rm -rf yarn.lock && yarn install && yarn build

Hope it help to following guys 🤗

Hey guys, try this to see if it works:

  1. Remove file postcss.config.js, and add a new file postcss.config.json with the same config in JSON format:
{
  "plugins": {
    "tailwindcss": {},
    "autoprefixer": {}
  }
}
  1. rm -rf .next && npm run dev

Worked around for Tailwind and PostCSS by forcing CJS in the type: module project with .cjs file extension for postcss.config.cjs and tailwind.config.cjs.

Just opened a PR to fix this issue: #63109

Just upgraded to next@14.0.4 and postcss-load-config@5.0.2 and confirmed that the bug still remains, ESM and TypeScript PostCSS formats are still not supported:

@BB-19 Thank you 🙏 that’s the only way I could make my website work. Why is renaming to postcss.config.cjs not sufficient though?

@karlhorky Thanks for testing. Yes, *.config.ts is out of scope of the PR I created this time and not supported yet, unfortunately.

PostCSS and Tailwind do not support ESM config files

@ctjlewis This is not true anymore, both support ESM:

Tailwind CSS config files can also be written in ESM syntax with TypeScript - and this already works now with Next.js.

As far as I can tell, this is a problem with Next.js or some other dependency in between.

This is a dealbreaker for me, I need my postcss config to be a module because I share an ESM js dependency between config and code (custom media queries), would be great to hear if it’s at least on the roadmap to be fixed