react - using CSS Modules in a Nrwl NX workspace

See original GitHub issue

I run version 8.4.12 of nx

I would like use css modules in a react app

In a recent react project, created with “create-react-app”, I am able to use css modules.

e.g. import classes from './MyComponent.module.css';

and the usage:

<div className={classes.my-css-class}> </div>

How is this possible with nx?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:23 (6 by maintainers)

github_iconTop GitHub Comments

12reactions
lihongwangcommented, Jan 10, 2022

Any update about lib support SCSS modules?

9reactions
jaysoocommented, Aug 29, 2019

Hi @maku

We don’t currently support CSS modules but we can look at extending our builder for it.

The workaround right now is to provide your own custom webpack to the web builder.

Look in your workspace.json file for your app and you should find a build entry under architect.

  "build": {
    "builder": "@nrwl/web:build",
    "options": {
      "differentialLoading": false,
      "outputPath": "dist/apps/demo",
      "index": "apps/demo/src/index.html",
      "main": "apps/demo/src/main.tsx",
      "polyfills": "apps/demo/src/polyfills.ts",
      "tsConfig": "apps/demo/tsconfig.app.json",
      "assets": ["apps/demo/src/favicon.ico", "apps/demo/src/assets"],
      "styles": ["apps/demo/src/styles.css"],
      "scripts": [],
      "webpackConfig": "@nrwl/react/plugins/babel"
    }

You need to switch webpackConfig with your own, such as "./apps/demo/webpack.config.js". Please change demo to your actual app name.

Now, you need to provide the config function like this:

https://gist.github.com/jaysoo/431828f47e5202d3e4afc6c34fdac2c0

This function modifies the webpack config by adding a rule to handle .module.css files, as well as exclude these files from other rules.


There will be more configuration beyond that, especially for production builds, postcss (if you use that), etc.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I use CSS Modules in a Nrwl NX workspace?
I'm refactoring an old React/Webpack project to Nrwl's NX monorepo solution. In that project css modules are used.
Read more >
@nrwl/react:library | Nx
Create a React Library for an Nx workspace. ... nx g @nrwl/react:library . ... the stylesheet is generated using global CSS instead of...
Read more >
Add Styling to a React app inside an Nx workspace | egghead.io
In this lesson we're going to explore how to add some more styling to our React app inside the Nx workspace. We're exploring...
Read more >
Using Nx Workspace generators to scaffold new blog posts
This week we'll continue by looking at how to leverage Nx Workspace generators to ... CSS file for the React component, especially if...
Read more >
How to use custom webpack configuration in an Nx project?
How to customize webpack config for an NX project? · Go to the workspace.json file · Find the project you wish to edit...
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