Skip to content
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

Global CSS cannot be imported from within node_modules #146

Closed
2 tasks done
samuelcastro opened this issue Dec 9, 2020 · 30 comments
Closed
2 tasks done

Global CSS cannot be imported from within node_modules #146

samuelcastro opened this issue Dec 9, 2020 · 30 comments
Milestone

Comments

@samuelcastro
Copy link

samuelcastro commented Dec 9, 2020

  • 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: vercel/next.js#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-modules version: 6.0.0
  • Node.js version: 14.4.0
  • npm/yarn version: 1.22.4
  • Operating System: macOS
  • Webpack 4

Any thoughts?

@samuelcastro
Copy link
Author

I upgraded the library and now I'm getting another issue, I'll investigate more first, closing for now.

@samuelcastro
Copy link
Author

samuelcastro commented Dec 9, 2020

I'm re-opening the issue, it is happening on v6 as well.

@samuelcastro samuelcastro reopened this Dec 9, 2020
@martpie
Copy link
Owner

martpie commented Dec 28, 2020

Where are you trying to import this CSS? From a component or _app?

@samuelcastro
Copy link
Author

Hi @martpie I'm importing them from a component, it works fine importing them from _app.

@martpie
Copy link
Owner

martpie commented Jan 13, 2021

next-transpile-modules mimicates the behavior of Next.js, so importing global css from components is not supported. Use _app instead.

@martpie martpie closed this as completed Jan 13, 2021
@samuelcastro
Copy link
Author

samuelcastro commented Jan 13, 2021

What do you mean @martpie? Next.js supports css import from components since 9.5.4, see: https://nextjs.org/docs/basic-features/built-in-css-support#import-styles-from-node_modules

@martpie
Copy link
Owner

martpie commented Jan 13, 2021

Ah good to know, so it's on my side 👍

@martpie martpie reopened this Jan 13, 2021
@nhducit
Copy link

nhducit commented Jan 18, 2021

@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
adobe/react-spectrum#1156

Workaround: use @zeit/next-css which nextjs used along with next-transpile-module
https://github.com/devongovett/rsp-next

@samuelcastro
Copy link
Author

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

@nhducit
Copy link

nhducit commented Jan 21, 2021

@samuelcastro you still can import 3rd css to your next component but, a library component cannot import it own css

// node_modules/@react-spectrum/actiongroup/dist/module.js
import "./main.css"; <=== this is not allowed

@martpie
Copy link
Owner

martpie commented Jan 21, 2021

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.

@nhducit
Copy link

nhducit commented Jan 21, 2021

@martpie nextjs team drop support this import style and require ui libraries change the way their import component css

// node_modules/@react-spectrum/actiongroup/dist/module.js
import "./main.css"; <=== this is not allowed

// component in next.js
import {
  Accordion,
  AccordionItem,
  AccordionButton,
  AccordionPanel,
} from "@reach/accordion";
import "@reach/accordion/styles.css"; <<=== recommended

@martpie
Copy link
Owner

martpie commented Jan 21, 2021

Do you have a link about that? What's the recommended way then?

@nhducit
Copy link

nhducit commented Jan 21, 2021

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

const withPlugins = require('next-compose-plugins')
const withCSS = require('@zeit/next-css')
const withTM = require('next-transpile-modules')([
  '@adobe/react-spectrum',
  '@spectrum-icons/.*',
  '@react-spectrum/.*'
])

module.exports = withPlugins([withCSS, withTM], {
  // Your next configuration
})

@nhducit
Copy link

nhducit commented Jan 21, 2021

Do you have a link about that?

@martpie checkout this link.

This is the error when using react-spectrum with next.js v10

error - ./node_modules/@react-spectrum/actiongroup/dist/main.css
Global CSS cannot be imported from files other than your Custom <App>. Please move all global CSS imports to src/pages/_app.tsx. Or convert the import to Component-Level CSS (CSS Modules).
Read more: https://err.sh/next.js/css-global
Location: node_modules/@react-spectrum/actiongroup/dist/module.js

adobe/react-spectrum#1156

@Naterra
Copy link

Naterra commented Jan 26, 2021

Same issue with CKEditor 5
What can we do to solve it?

image

image

@martpie
Copy link
Owner

martpie commented Jan 26, 2021

So 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:

  • the most useful thing is to submit a failing test, that way I can experiment to fix the rest and then I know it will work for your usecase
  • otherwise, a real fix is always welcome

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!

@martpie
Copy link
Owner

martpie commented Feb 1, 2021

Ok, So I've tested things a little bit:

  • importing a global CSS file from node_modules in a component in your Next.js app works
  • importing a CSS file from you Next.js app in a component in your Next.js does not work
  • Importing the same file from _app works

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.

@nhducit
Copy link

nhducit commented Feb 1, 2021

there are two more cases:

@samuelcastro
Copy link
Author

Ok, So I've tested things a little bit:

  • importing a global CSS file from node_modules in a component in your Next.js app works
  • importing a CSS file from you Next.js app in a component in your Next.js does not work
  • Importing the same file from _app works

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.

The first scenario never worked for me. (importing a global CSS file from node_modules in a component in your Next.js app works)

@KB1RMA
Copy link

KB1RMA commented Feb 15, 2021

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-css finally.

@martpie
Copy link
Owner

martpie commented Feb 15, 2021

Thank you for testing the branch and confirming it was working for your use-case, I will try to complete it soon.

@karol-f
Copy link

karol-f commented May 17, 2021

@martpie Can I help You somehow with e.g. testing this feature?

@martpie
Copy link
Owner

martpie commented May 17, 2021

@karol-f if you can:

  • describe your usecase (if it's similar to the first message of this issue or different)
  • test Global CSS improvements #166 and tell me if it solves your usecase

those two things would help a lot

edit: it seems that my rebase with master broke it, so let me fix the checks first

@karol-f
Copy link

karol-f commented May 17, 2021

@martpie

Use-case:

  • I'm using NPM Workspaces (monorepo)
  • Used to tweak next.config.js file to use other monorepo packages (using webpackConfig.module.rules[0].include = [...]) but switched to next-transpile-modules
  • With the newest Next.js releases @zeit/next-css and @zeit/next-sass packages are deprecated
  • You are forced to remove the above packages when switching to Webpack 5 version of Next.js

And the problem is that without e.g. @zeit/next-css package, all global CSS imports in other monorepo packages (that are imported in your Next.js app) results in error mentioned in issue description

Global CSS cannot be imported from within node_modules.
Read more: https://err.sh/next.js/css-npm

So basically next-transpile-modules is currently the only solution for me to use both monorepo and Next.js.

P.S. I'll try to run and test the mentioned branch. Thank You for preparing that!

@martpie
Copy link
Owner

martpie commented May 17, 2021

Sorry, I was not precise enough:

all global CSS imports in other monorepo packages (that are imported in your Next.js app)

Can you detail this in particular? Your setup, how your files are organized, are you using exactly the same convention as Next.js with .module.css, etc etc?

@karol-f
Copy link

karol-f commented May 17, 2021

@martpie Ok, I run some tests on 7c1039bbcc5cc316c53c7222cbc38bde5b61ce70 commit (global-css-improvements branch) and there are still errors about global CSS.

My project folder structure is something like this:

.
└── packages
    ├── components
    │   ├── map
    │   │   └── Map.tsx --> import './some-local-styles.css'
    │   └── media
    │       └── Picker.tsx --> import 'react-image-crop/dist/ReactCrop.css' 
    ├── search-widget
    │   └── Datepicker.tsx --> import 'react-datepicker/dist/react-datepicker.css'
    ├── next.js app
    └── other non-next.js app consuming componets

Next.js app is one of a few that consume components and search-widget packages. This packages:

  • use styled components (no problem)
  • import local CSS (problem)
  • import CSS from NPM dependencies like react-datepicker(problem)

As these components are used also in other apps that are non-next.js, the .module.css approach is not something I consider now.

Example error on 7c1039bbcc5cc316c53c7222cbc38bde5b61ce70 commit:

../../node_modules/react-datepicker/dist/react-datepicker.css
Global CSS cannot be imported from files other than your Custom <App>. Please move all global CSS imports to pages/_app.tsx. Or convert the import to Component-Level CSS (CSS Modules).
Read more: https://nextjs.org/docs/messages/css-global
Location: ../search-widget/components/DateRangePicker/DateRangePicker.tsx

Regards!

@martpie
Copy link
Owner

martpie commented May 17, 2021

Ok, so let's ignore styled-components, they're irrelevant.

next-transpile-modules is about applying the existing Webpack rules of Next.js, to some specific path in node_modules, nothing more. In other words, if your usecase is not covered by Next.js, NTM won't try to fix it.

  • import CSS from NPM dependencies like react-datepicker: this I can see is supported there, so this is a lack of support from NTM that I should fix
  • import local CSS: last time I checked, 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 it

@martpie martpie added this to the 7.x milestone May 18, 2021
@karol-f
Copy link

karol-f commented May 21, 2021

Workaround for now, if you are using something like styled components in packages consumed by Next, you can use something like this:

import datePickerCss from '!!raw-loader!react-datepicker/dist/react-datepicker.css'; // load CSS as a string into variable
import { Global } from '@emotion/core'; // You can use solutions from styled components or material-ui if you are using it

and

<Global styles={{ datePickerCss }} />

Works for me.

@martpie
Copy link
Owner

martpie commented May 21, 2021

Support for global CSS was added by @fabianishere in 7.2.0 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants