question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Importing correctly scoped css from node_modules outside of _app

See original GitHub issue

Bug report

Describe the bug

Importing a stylesheet from a package is not possible in a page, because next throws with this error:

Global CSS cannot be imported from files other than your Custom <App>. Please move all global CSS imports to pages/_app.tsx.
Read more: https://err.sh/next.js/css-global

While I understand where this stems from, it makes code-splitting impossible. If I import a component from a component library, I need to import the CSS as well. There might be libraries that don’t correctly scope their selectors, but that shouldn’t stop me from overwriting this warning. CSS that is imported from a library isn’t inherently “global”.

To Reproduce

  1. import "my-library/index.css"
  2. yarn dev
  3. I get the error from above

Expected behavior

The file should be imported.

I see these possible solutions:

  • Global flag in next.config.js
  • Comment annotation on import
  • Warn instead of completely blocking it
  • Check if the CSS only contains scoped selectors

Additional context

There have been previous discussions about this.

From the blog

Since stylesheets are global by nature, they must be imported in the Custom <App> component. This is necessary to avoid class name and ordering conflicts for global styles.

I disagree with this statement, the reasoning being that an external library can use CSS modules and package them as a CSS file to import. Which is perfectly valid and common practice and does not have side effects.

#10059

This issue got closed because the global import in _app is the correct choice there. This comment describes the exact problem, but there hasn’t been any response, as the issue is closed. The comment got a lot of positive reactions though, so I suppose I’m not the only one with this problem.

#10975

Seems to be unrelated.

#9830

Might be related, but I’m not sure.

My use case

I write long articles with a lot of custom artwork and interactive illustrations. Articles use private npm packages with react components that render SVG with quite a bit of CSS. These packages use CSS modules and export an index.js and index.css. Adding all the CSS files to _app causes all the CSS to be loaded, even if people are on the home page, the contact form, or any other article, even though it’s 100% unused. It also goes against having the file system take care of your pages because almost every page corresponds to a CSS import in _app.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:103
  • Comments:60 (7 by maintainers)

github_iconTop GitHub Comments

72reactions
Timercommented, Aug 24, 2020

We’re going to be allowing importing CSS from node_modules into any component file within the next week (on canary)! We’ll post here when it’s ready to test.

32reactions
harrisossermancommented, Jun 19, 2020

+100 to this. I’m having to copy and paste node module css files into my project and adding a .module.css on them

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolving @import .css in SCSS from node modules in Nuxt.js ...
For me the ~@ seems to make more sense, as ~ resolves to the path of my node_modules and the actual folder name...
Read more >
Getting Started - BootstrapVue
Check out what is new in BootstrapVue release v2.23.0. ... Vue Guide · Vue API · Bootstrap v4.6 documentation · Vue loader scoped...
Read more >
How to Use Sass with CSS Modules in Next.js - freeCodeCamp
CSS Modules are essentially CSS files that, when imported into JavaScript projects, provide styles that are scoped to that particular part of ...
Read more >
CSS | single-spa - JS.ORG
Re-export the components from the design system so that all other microfrontends can access them via cross microfrontend imports ( import { Button...
Read more >
How to Import a Sass File into Every Vue Component in an App
How? · You will need to shut down and restart your local development server to make any of these changes take hold. ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found