next-transpile-modules: Global CSS cannot be imported from within node_modules
- I HAVE READ THE FAQ AND MY PROBLEM WAS NOT DESCRIBED THERE
- I WILL GIVE 10$ TO CHARITY IF MY SOLUTION WAS ACTUALLY IN THE FAQ
Are you trying to transpile a local package or an npm package? local package
Describe the bug
My transpiled UI library imports a css file from react-datepicker, and this is throwing an error which is described below. Next.js 10.x now supports the ability to import css from 3rd party modules without the need to include it in the _app.tsx (https://nextjs.org/docs/basic-features/built-in-css-support#import-styles-from-node_modules).
Initially I thought this was a bug on Next.js then I opened up this bug: https://github.com/vercel/next.js/issues/19936, although @timneutkens suggested me that this might be a bug on this library instead.
This is the error I’m getting:
./node_modules/react-datepicker/dist/react-datepicker.css
Global CSS cannot be imported from within node_modules.
Read more: https://err.sh/next.js/css-npm
Location: node_modules/@edooking/ui/src/elements/DatePicker/DatePicker.tsx
To Reproduce Import a css in a transpiled library.
Expected behavior Be able to import the css and successfully run the application.
Setup
- Next.js version: 10.0.3
next-transpile-modulesversion: 6.0.0- Node.js version: 14.4.0
npm/yarnversion: 1.22.4- Operating System: macOS
- Webpack 4
Any thoughts?
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 22
- Comments: 30 (12 by maintainers)
I’m re-opening the issue, it is happening on v6 as well.
Support for global CSS was added by @fabianishere in 7.2.0 🙌
Ok, so let’s ignore
styled-components, they’re irrelevant.next-transpile-modulesis about applying the existing Webpack rules of Next.js, to some specific path innode_modules, nothing more. In other words, if your usecase is not covered by Next.js, NTM won’t try to fix it.I don’t believe it was possible in Next.js, so, to me, the solution is to support CSS modules in node_modules, but not “local CSS”, would that be ok for you?Edit: yeah ok, I see, conflict with the first point, so I guess we should support itThank you for testing the branch and confirming it was working for your use-case, I will try to complete it soon.
I don’t think you’re right @nhducit, Next.js still supports 3rd party css import, see: https://nextjs.org/docs/basic-features/built-in-css-support#import-styles-from-node_modules
FWIW I’ve given #166 a tryout on our large project and it’s working great with global CSS in our internal modules.
Appreciate the work on that - it’s great to be able to ditch
next-cssfinally.Ok, So I’ve tested things a little bit:
So this is getting super weird and shady and I am still not sure if this is a bad or good idea to support this scenario.
@martpie checkout this link.
This is the error when using react-spectrum with next.js v10
https://github.com/adobe/react-spectrum/issues/1156
@samuelcastro you still can import 3rd css to
yournext component but, a library component cannot import it own css@samuelcastro @martpie from nextjs v10, nextjs stop support third 3rd like react-spectrum import css.
I think we can close this issue or add a note in readme about this issue
check out this issue in react-spectrum https://github.com/adobe/react-spectrum/issues/1156
Workaround: use
@zeit/next-csswhich nextjs used along with next-transpile-module https://github.com/devongovett/rsp-nextSo right now I’m in a sabbatical, so open-source support is limited 😝
I will try to have a deeper look tomorrow.
In general, if anyone has the time to work on anything on this project, there are two things you can help me with:
If you get lost in the codebase, or you are afraid to dive in, know that the first point is the best thing you can do, as it will probably save me 80% time.
Cheers!
I’m using this workaround, use the old and deprecated withCSS plugin, combine with next transpile module
https://github.com/devongovett/rsp-next/blob/master/next.config.js
Do you have a link about that? What’s the recommended way then?
This is indeed a gray area and I am not 100% sure if adding/fixing that would not break some Next.js optimizations.
I’d need to talk to Vercel a little bit.