Using ReactJS with Typescript and react rewired to load CSS module

I am currently setting up a proof of concept project using ReactJS and typescript, and I want to incorporate CSS modules without ejecting the webpack configuration. Here are the steps I have taken so far:

  • Installed create-react-app globally - npm install -g create-react-app
  • Created a new project named 'firstapp' with TypeScript support - create-react-app firstapp --scripts-version=react-scripts-ts
  • Installed react-app-rewired as a development dependency - npm install react-app-rewired --save-dev
  • Installed necessary packages for CSS modules - npm install --save-dev codebandits/react-app-rewire-css-modules sass-loader node-sass
  • Modified package.json scripts to use react-app-rewired - "start": "react-app-rewired start --scripts-version react-scripts-ts"
  • Created a config-overrides.js file to apply css modules - const rewireCssModules = require('react-app-rewire-css-modules'); module.exports = function override(config, env){ config = rewireCssModules(config, env); return config; }

I have set up my App.module.scss file correctly and imported it in my App.tsx file:

import styles from './App.module.scss';

However, when I run the project, I encounter an error related to the css module: "Cannot find module './App.module.scss'."

The project works fine without TypeScript setup. What changes should I make to enable CSS modules?

I found typings-for-css-modules-loader, but I am unsure how to integrate it without ejecting the webpack configuration.

Thank you,

Thomas .T

EDIT 1: I created a global.d.ts file with:

  • declare module '*.css'

  • declare module '*.scss'

This resolved the issue without using typings-for-css-modules-loader in the end.

For detailed information, refer to this article:

Answer №1

I created a global.d.ts file containing:

  • declare module '*.css'
  • declare module '*.scss'

This solution worked for me without using typings-for-css-modules-loader.

You can find more details in this article:

I will mark this as the accepted answer in 2 days.

Answer №2

After extensive research, I was able to find a workaround. As someone new to React JS and attempting to use TypeScript for building, I encountered an issue with importing CSS modules. Despite using import './button.css' instead of import class from './button.css', I faced numerous CSS conflicts where higher component scope styles were consistently overridden by lower components. This led me to search for a solution.

My solution involved utilizing the react-script-ts package.

The Solution: With the release of create-react-app version 2.1, TypeScript support was introduced. To address the problem, here are the steps to upgrade your application:


1. Create a new application using `npx create-react-app my-new-app --typescript`
2. Replace your old src folder in the new solution
3. Rename all CSS files with `*.module.css` and adjust imports accordingly.
4. Ensure all necessary packages dependencies are added
5. Run your project

For more detailed information on this solution, you can refer to the source:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When viewed on a mobile device, the entire HTML document is resized to occupy less than half of the

Whenever I adjust the size of my webpage on different devices or mobile phones, I notice that the <div> tag appears to be only half (or even less) the width of the page. This gap seems to increase as the window size decreases. Refer to the screenshot ...

Contrast between pre-tag in HTML and white-space: pre newline exclusion

I originally thought that the pre(html tag) would act just like 'white-space:pre'. However, it turns out that it does not behave in the same way. <pre> aaa bbb </pre> <p style="white-space:pre;"> aaa bbb </p> The <pr ...

Is it possible to generate distance between 2 elements without utilizing padding or margin?

In my React Native project, I'm currently working with 2 inline buttons - the search and add buttons: https://i.stack.imgur.com/tKZrf.png I'm looking to add some spacing between these buttons without impacting their alignment with the left and ...

Call a React component from an external JavaScript file

I am working on a react application that has a bundle.js file created using webpack containing all the necessary code. Recently, I started developing a separate dotnet application and I need to display the main 'App' component from my react appl ...

What is the best way to change routes within my React application?

I am currently facing a challenge with the Routes in my work's web application. The existing method of accessing routes involves manually typing in the extended path in the browser, which is not ideal. I want to enhance this by adding new routes that ...

The benefits of a modular design in a React and TypeScript library

Following a tutorial on creating a library with React, Typescript, and rollup, I successfully managed to get everything working. However, as my project grows with additional features, I'm looking to have modular exports similar to what is seen in redu ...

Is there a way to implement imports based on a specific condition?

I'm just starting to learn Angular and TypeScript. My goal is to redirect multiple hostnames to a single Angular 6 project because most aspects are the same, with only language and URLs varying. For example, here's what I have implemented in ap ...

Transfer the React.js application from the /dist directory to Heroku using GitLab for deployment

Trying to move the dist folder of a project from GitLab to Heroku using GitLab CI: image: node:8.10.0-alpine cache: key: "alpine" paths: - node_modules/ stages: - build - production build: stage: build artifacts: paths: - dist/ ...

Manipulating, modifying, and verifying distinct information rather than inputting to / retrieving from the model through the use of Reactive Forms

My configuration I am retrieving a value from the database as a number. This value must always be a number, except when displaying it in an input element and validating user input. In those cases, the number needs to be a HEX value. Here is the desired p ...

Adding a Row in Material Table position

After clicking Add Row in the material table, a new edit row is currently displayed at the bottom of the table. This can be quite inconvenient when there are multiple rows per page as the user would need to scroll down each time to add anything. To improv ...

Getting Session from Next-Auth in API Route: A Step-by-Step Guide

When printing my session from Next Auth in a component like this, I can easily see all of its data. const session = useSession(); // ... <p>{JSON.stringify(session)}</p> I am facing an issue where I need to access the content of the session i ...

Exploring the seamless integration of Material UI Select with react-hook-form

I've put together a form in React with Material UI and React Hook Form. I'm attempting to develop a customized TextField component that functions as a Select Input. My goal is for it to be an uncontrolled element with a Ref prop. Despite followin ...

Guide to integrating Wav2Lip into ReactJS

I am looking to integrate the Wav2Lip Python package into ReactJS using NodeJS. While searching on NPM, I came across a package called wav2lip, but unfortunately, it is not compatible with ReactJS. I attempted editing the webpack using craco, but my effort ...

Dividers afloat - expanding current script with multiple additions

I am currently utilizing this website as a hub for showcasing my various web projects. The jsfiddle code provided in response to this particular inquiry is exactly what I need, however, I am unsure of how to have multiple divs moving simultaneously acros ...

Setting up AngularJS 1.5.x to function seamlessly with SystemJS and TypeScript

I'm looking to keep all my code in AngularJS 1.x while also preparing it for an easy upgrade to AngularJS 2 in the future. To align my code with Angular 2 standards, I am interested in using TypeScript and SystemJS in version 1.5.x initially. Is ther ...

What is causing the background image to not display within my <div>?

I want to add a background image to only a specific part of my website, using the image as an id called "fruit." However, the image is not showing up. Here is the code I have: <div class="p-1 text-center bg-light" id="fruit"> &l ...

Is there a way to refresh CSS styles when the window width is adjusted?

Is there a way to refresh CSS styles when the window width changes? I attempted this method, but unfortunately it did not work as expected. A simple refresh (F5) helps to rectify the CSS tags. jQuery(function($){ var windowWidth = $(window).width(); ...

Synchronizing two navigation menus on a single-page application website

Let me start by saying that I specialize in back end development and am facing a specific challenge with building a website that includes two navigation menus. The main navigation menu features links like Home, while the sub-navigation menu includes option ...

"Implementing a responsive design with a background image and radial gradient - how to

The image currently in use: https://i.stack.imgur.com/piK6l.jpg Here is how I've implemented it using Vue.js, Bootstrap, and CSS: Code Snippet <div class="landing"> <div class="container text-white details h-100"> ...

Having trouble displaying an image in the background using CSS? Look no further than Laravel

After running the command php artisan storage:link, I am facing an issue where the image does not load. Upon inspection, it displays a message saying "Could not load the image". https://i.sstatic.net/KKFy9.png What adds to my confusion is that ...