-
Notifications
You must be signed in to change notification settings - Fork 27k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
next/lib/find-config
does not support ESM config files
#34448
Comments
next/lib/find-config.js
: add ESM interop support for type: module
next/lib/find-config
: add ESM interop support for type: module
Next no longer throws with the changes in #34451, but Tailwind will need to make changes in order for Tailwind to not throw:
|
next/lib/find-config
: add ESM interop support for type: module
next/lib/find-config
does not support ESM config files
Worked around for Tailwind and PostCSS by forcing CJS in the |
If anyone is struggling with TailwindCSS + "type: module":
|
@ctjlewis I'm not sure this works? At least from what I can tell from the way @BB-19 I'm not sure this can work for filtering plugins against |
@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? |
@BB-19 is not working for me, what version of next are you using? error - ./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
/Users/damians/Desktop/predator/tailwind.config.cjs:29
export {};
^^^^^^
SyntaxError: Unexpected token 'export'
at compileFunction (<anonymous>)
Ready on http://localhost:80 |
@damianobarbati You can’t use |
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 |
I was receiving the same error myself when using
The reason why "export {}" is added is explained here. Namely that,
This might only be the case if you set My guess for why adding this to ts-node's ignore field is required is that normally with just |
@BB-19 you saved me, but why does this work? |
PostCSS and Tailwind do not support ESM config files. You must rename them to .cjs. There's no need to move postcss config to package.json and I don't recommend that. |
- 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. Referencing variables will not work for anything used in NextJS as it is loaded through `dotenv` (so we can use `.env` in parent directory), which does not seem to work with variable references (such as jddAqAqAq`A=$B`)q - Restructured code to work with NextJS - Fixed bug with tailwind: vercel/next.js#34448 (comment) - Added dependencies (`dotenv`, `eslint-config-next`, `next`)
It's magical !!! |
@BB-19, No need to rename {
...
"type": "module",
"postcss": {
"plugins": {
"tailwindcss": {},
"autoprefixer": {}
}
},
...
} For me, Yarn Berry with PnP Strict, the solution for convert
Hope it help to following guys 🤗 |
@Mon-lus Thanks a lot. It worked for me. It would be helpful if you could explain why the error happened. Once again thanks a lot |
@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. |
In the meantime, before Next.js supports ESM in PostCSS config files, I've opened the following PR to rename |
Just upgraded to
|
Hey guys, try this to see if it works:
{
"plugins": {
"tailwindcss": {},
"autoprefixer": {}
}
}
|
@backslash112 I just tried |
We want to use It is not working now. |
Just opened a PR to fix this issue: #63109 |
<!-- 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 checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # --> ### What? Prevent confusing error messages when changing to `"type": "module"` in `package.json` ``` ./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. ``` ### Why? Even though PostCSS itself [supports ESM and TypeScript configuration files](postcss/postcss-load-config#230), Next.js itself does not (because of `next/lib/find-config`): - #34448 ### How? By switching to `.cjs`, the config will stay recognized as CommonJS even after switching to `"type": "module"` in `package.json` cc @balazsorban44 --------- Co-authored-by: Sam Ko <sam@vercel.com>
Fixes #34448 Before this PR, next/lib/find-config fails to load \*.config.mjs files and \*.config.js files when `"type": "module"` is set in package.json. It expects CommonJS files although the \*.config.{js|mjs} files are written in JS modules format (i.e. using `import` and `export`). This PR fixes it so that it can load configs written in JS modules format. --------- Co-authored-by: Jiachi Liu <inbox@huozhi.im>
Thanks for the PR for ESM support @phanect and for the review and merge @huozhi 🙌 I can confirm that
I left the 1. CommonJS project +
|
One thing that is NOT yet supported is the
It looks like the compilation works, but this results in an unstyled application. But since this was not the main request by @ctjlewis in this issue, and appeared later in the issue comments, it's probably best to open a new issue for this. |
One thing that probably could be still improved here is altering I opened a PR for this here: |
@karlhorky Thanks for testing. |
So, right now we can use |
@SalahAdDin Yes, the latest canary release should support *.config.mjs now. |
<!-- 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 checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change --> ### What? Now that @phanect added support for ESM PostCSS config files in PR #63109 (original issue #34448), PostCSS can use ESM config by default. It needs to use an `.mjs` extension by default because `create-next-app` scaffolds CommonJS apps by default. This will also work with ESM projects which have added `"type": "module"` in their `package.json` ### Why? 1. To convert one more file to ESM 2. To use the modern format 3. To follow other similar migrations that have taken place in the Next.js codebase (eg. `next.config.mjs`) ### How? - Change file extensions from `.cjs` to `.mjs` (change similar to PR #58380) - Change module format from CommonJS to ESM - Add type for the config, for users who enable `checkJs: true` in `tsconfig.json` Co-authored-by: Sam Ko <sam@vercel.com>
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
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: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 runnext dev
.The text was updated successfully, but these errors were encountered: