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
- Migrate to NextJS - Update `.env` file to convert VITE to NEXT_PUBLIC - IMPORTANT: `.env` files will need to be updated to reflect this, please check the .env.example for more information. Referenc... — committed to jacob-horton/flipster by jacob-horton a year ago
- use commonjs for PostCSS config file because tailwind doesnt support ESM. https://github.com/vercel/next.js/issues/34448 — committed to ryoikarashi/ryoikarashi.com by ryoikarashi 5 months ago
- use commonjs for PostCSS config file because tailwind doesnt support ESM. https://github.com/vercel/next.js/issues/34448 — committed to ryoikarashi/ryoikarashi.com by ryoikarashi 5 months ago
- Switch to .cjs PostCSS config (#58380) <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the ch... — committed to vercel/next.js by karlhorky 4 months ago
- Add JSM (ESM) support for next/lib/find-config (#63109) Fixes #34448 Before this PR, next/lib/find-config fails to load \*.config.mjs files and \*.config.js files when `"type": "module"` is set i... — committed to vercel/next.js by phanect 3 months ago
- Switch to postcss.config.mjs, add type (#63380) <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you foll... — committed to vercel/next.js by karlhorky 3 months ago
If anyone is struggling with TailwindCSS + “type: module”:
@BB-19, No need to rename
tailwind.config.jstotailwind.config.cjs. The problem ispostcss.config.js, remove it and add the config topackage.json. For exampleFor me, Yarn Berry with PnP Strict, the solution for convert
nextproject from CommonJS to ESModule:package.jsonwith “type”: “module”postcss.config.jsand apply it topackage.jsonwith “postcss” json format, see above examplenext.config.jsandtailwind.config.jsto ESM format.yarnrc.ymlyarn cache clean && rm -rf yarn.lock && yarn install && yarn buildHope it help to following guys 🤗
Hey guys, try this to see if it works:
postcss.config.js, and add a new filepostcss.config.jsonwith the same config in JSON format:rm -rf .next && npm run devWorked around for Tailwind and PostCSS by forcing CJS in the
type: moduleproject with.cjsfile extension forpostcss.config.cjsandtailwind.config.cjs.Just opened a PR to fix this issue: #63109
Just upgraded to
next@14.0.4andpostcss-load-config@5.0.2and confirmed that the bug still remains, ESM and TypeScript PostCSS formats are still not supported:postcss.config.mjs(orpostcss.config.jswith ESM in"type": "module"projects)postcss.config.tsTypeScript config also supported by PostCSS@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.
@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