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.

Global CSS cannot be imported from within node_modules.

See original GitHub issue

Bug report

I can’t import a css file from a node_modules 3rd party library as described here (https://nextjs.org/docs/basic-features/built-in-css-support#import-styles-from-node_modules).

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

It seems this was fixed before here https://github.com/vercel/next.js/issues/12079, but since I’m able to reproduce the issue again on Next.js 10.0.3 I’m re-opening the issue.

To Reproduce

Try to import a CSS from a 3rd party lib.

Expected behavior

To be able to import css

System information

  • OS: macOS
  • Chrome
  • Version of Next.js: 10.0.3
  • Version of Node.js: 14.4.0
  • Deployment: vercel

Additional context

https://github.com/vercel/next.js/issues/12079

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:28
  • Comments:20 (5 by maintainers)

github_iconTop GitHub Comments

66reactions
HitsiLentcommented, Apr 1, 2021

how can you control all 3rd part developer don’t let them import css to their own library, that’s don’t make sense.

56reactions
timneutkenscommented, Dec 8, 2020

This is expected, this particular library is incorrectly publishing css imports to npm, looks like it even publishes JSX. Instead it should be exposed separately:

Location: node_modules/@edooking/ui/src/elements/DatePicker/DatePicker.tsx

Importing global css from node_modules is allowed like this in your code:

// Allowed
import 'react-datepicker/dist/react-datepicker.css'

// Not allowed
import DatePicker from '@edooking/ui/src/elements/DatePicker/DatePicker' // this then imports the css

Main reasons behind this are that we don’t compile node_modules further, meaning those files can’t be found during pre-rendering and would crash your application. It’s also not compatible with esmodules so looking to the future it’ll cause issues.

An example of correctly publishing a library like this is https://reach.tech/styling/

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.js Global CSS cannot be imported from files other than ...
I came to this answer because I'm trying to use scss inside a component. and apparently now I need to separate my scss...
Read more >
Global CSS cannot be imported from within node_modules
18 Day 11 77. Combinations Given two integers n and k, return all possible combinations of k numbers out of the range [1,...
Read more >
Next.js: Global CSS cannot be imported from files other than ...
When migrating to Next.js 9+, I was running into an issue where I could not import global styles from any file other than...
Read more >
Next.js Global CSS cannot be imported from files other than ...
CSS : Next.js Global CSS cannot be imported from files other than your Custom App [ Beautify Your Computer ...
Read more >
Using CloudScape with NextJS - Andrew Stacy
node_modules/@cloudscape-design/components/alert/styles.scoped.css Global CSS cannot be imported from within node_modules.
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 Hashnode Post

No results found