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.

Allow importing CSS modules from other NPM packages

See original GitHub issue

Feature request

Allow importing CSS modules from other NPM packages.

Is your feature request related to a problem? Please describe.

An NPM package is the primitive of code sharing. Therefore monorepo is very common to split the domain into separate NPM packages. It feels very wrong when we can’t import React components.

It should be possible to import CSS files from packages that follow the CSS modules convention *.module.(css|scss) because Next.js ensures already that CSS Modules are side-effect free.

The current solution forces me:

  • Handle my private packages as “global-CSS”
  • Pre-compile all my components to not have a CSS import statement.

Current error:

error - ./node_modules/react-modern-calendar/dist/esm/components/Calendar/index.module.css
CSS Modules cannot be imported from within node_modules.
Read more: https://err.sh/next.js/css-modules-npm

Additional context

Next.js: 9.4.2

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:194
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

16reactions
lauriskuznecovscommented, Mar 18, 2021

I really appreciate that many things are new bundle in Next.js by default, but there are some cases which really blocks development.

Referring to this documentation, it should work if we import css from our component, instead of dependency importing it inside node_modules

Example:

  • My next app > Component A > Dependency A (react-bootstrap) - WORKS
// _app.js
import 'bootstrap/dist/css/bootstrap.css'
// Component A
import Button from 'react-bootstrap/Button';
  • My next app > Component A > Dependency A > Dependency B (react-bootstrap) - DOES NOT WORK
// _app.js
import 'Dependency-A/styles.css'
// Component A
import Button from 'my-custom-ui-lib/Button';
// Dependency A
import Button from 'react-bootstrap/Button';
import 'bootstrap/dist/css/bootstrap.css'
// My custom logic which makes some new component out of Bootstrap Buttons
12reactions
arshawcommented, Jul 30, 2020

I would very much like Next to support css modules for third-party libs, but I’ll hold off on expressing why. I just want to comment on the .module.css naming issue for now.

This sort of reminds me of the .js versus .mjs debate… how a file extension for essentially the same format changes the way in which others interpret it. .mjs was at least championed by the Node.js team, but as for interpreting a CSS file as either a local module or global, I can’t imagine any organization declaring a standard. It’s more a responsibility of the bundlers, and if any bundler was going to create a defacto standard for this, it would be Webpack…

…well it turns out Webpack already seems to have settled upon the .module.css extension. Take a look at the docs for the css-loader: https://webpack.js.org/loaders/css-loader/#auto . If you configure modules:true the modules{auto:true} is implied, which results in any filenames matching /\.module\.\w+$/i being interpreted as a local CSS modules.

I would kindly ask the Next team to consider going along with this standard! It would make my life as a third-party UI-based CSS-enabled package maintainer much easier!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Allow importing CSS modules from other NPM packages
It should be possible to import CSS files from packages that follow the CSS modules convention *.module.(css|scss) because Next.js ensures already that CSS ......
Read more >
how to export and import style in npm package? - Stack Overflow
1 Answer 1 · 3. For webpack, do npm install --save-dev css-loader . Then in your webpack.config.js, add this line to the use...
Read more >
Using CSS Module Scripts to import stylesheets - web.dev
Learn how to use CSS module scripts to import CSS stylesheets using the same syntax as JavaScript modules.
Read more >
postcss-import - npm
PostCSS plugin to transform @import rules by inlining content. This plugin can consume local files, node modules or web_modules. To resolve path ...
Read more >
css-loader - npm
Allows to enables/disables or setups number of loaders applied before CSS loader for @import at-rules, CSS modules and ICSS imports, i.e. @ ...
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