Gatsby - Caution! Import Error Detected: 'css' Does Not Have a Default Export (Imported as 'styles')

While starting with Gatsby, I encountered an issue in my app where running the command 'gatsby develop' resulted in a warning in the terminal:

warn Attempted import error: '../styles/home.module.css' does not contain a default export (imported as 'styles').

Subsequently, when attempting to load a page, it failed with the following runtime error:

Unhandled Runtime Error 
Cannot read property 'header' of undefined 
<section className={styles.header}>

The code snippet causing the issue is from my index.js file:

import styles from '../styles/home.module.css'

export default function Home({ data }) {
  return (
  <Layout>
     <section className={styles.header}>

Here is a portion of my CSS module from the home.module.css file:

.header {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-gap: 40px;
  align-items: center;
}

I am unsure what could be wrong with my CSS Module. Any insights?

Answer №1

Starting with Gatsby version 3, CSS modules must be imported as ES Modules:

import * as styles from './[componentName].module.css'

When implementing this in your code:

import * as styles from '../styles/home.module.css'

It's important to note that this method of importing CSS modules is not recommended, as it prevents webpack from optimizing your styles. Ideally, you should import specific named modules like this:

import React from "react"
import { box } from './mystyles.module.css'

const Box = ({ children }) => (
  <div className={box}>{children}</div>
)

For more information, visit:

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

color settings on a search icon as a background

I'm struggling to make the background color of my search glyph fill the entire box. I want it to resemble this image. However, at the moment, it looks more like this. I can't figure out what's causing the issue. Below is the HTML code I&a ...

Ways to resolve flickering caused by css keyframe animation

I'm currently working on creating a staggered opacity effect for three boxes using an infinite keyframe animation and the animation-delay property. However, I've encountered an unexpected issue where the third box seems to fade away and then rea ...

When floating a heading 4 (h4) and two divs to the left, and a single div to the right,

When floating a h4 and 2 divs to the left, and another div to the right, how can I place a block inside the remaining space that perfectly fills it? The background color of this space should not spread outside. [h4][div][div][remaining space][div] Thank ...

Customize the Bootstrap Navbar to display background only on the right side and not across the entire width

I am utilizing Bootstrap's navbar, specifically using the dropdown toggler icon on the right side. I am looking to have a background that only covers the right portion of the width and not the entire width. Any suggestions or insights would be greatly ...

Using regular expressions to enclose a JSON property value within a new string. Upgrading to MongoDB

For my testing purposes, I am attempting to transfer data from a custom JSON database to MongoDB. The data is currently stored in a json file with the following format: { "_id": "213124123114", "foo":"bar", "otherId": "2324242424", ... } To maintai ...

Steps to link two mat-autocomplete components based on the input change of one another

After reviewing the content on the official document at https://material.angular.io/components/autocomplete/examples I have implemented Autocomplete in my code based on the example provided. However, I need additional functionality beyond simple integrati ...

Tips for customizing text field appearance in Safari and Chrome

I haven't had a chance to test it on IE yet. My goal is to create a unique style for the background image and text box shape, along with borders, for the search bar on my website, [dead site]. If you check it out on Firefox or Opera and see the sear ...

Store in database and forward to a different web address

I have created an interactive quiz using jQuery. I need to add a specific feature at the end of the quiz. if (answered_questions === total_questions) { save the user's score to the database; redirect to specified URL; } The redirect_url is ...

The issue of objects within objects consistently yielding undefined when using $.each

Maybe it sounds a bit silly, but I am dealing with this status JavaScript object containing other objects (output of console.log(status)): {1: {…}, 2: {…}, 10: {…}} 1: error: true __proto__: Object 2: validated: false value: 0 wh ...

javascript - Alternate text colors several times within specified color ranges

Seeking assistance with changing the text color multiple times from #627CA9 to #FFFFFF and vice versa. Here's what I've tried: function changeColor(id) { var x = document.getElementById(id); if (document.getElementById(id).style.color = " ...

The position of the searchclear button has been shifted

My livesearch script is working well, but there seems to be a displacement issue with the searchclear button in the image below. I have two JavaScript scripts on my page and I loaded two CDNs, which I believe is causing this displacement. Can someone pleas ...

The setInterval function for the automated image slider is experiencing issues and is not functioning properly

I am facing an issue where the third photo in the slider does not appear initially. Eventually, the slide fails completely, causing the photos to change rapidly. Strangely, after the slide fails, the third photo sometimes appears sporadically. I am puzzl ...

When inline-block elements wrap onto a new line, they expand to full width

Upon my realization, an inline-block element will expand to 100% width if there are more child elements than can fit on one line: <div style="background-color:red;padding:5px"> <div style="display:inline-block;background-color:green;padding:5 ...

Utilize ng-show/ng-hide to dynamically display messages or content based on the results obtained

One of my functions builds and returns a JSON object like this: {"message":"No Grupos de MetaDetalles found","entities":[],"breadcrumbs":[],"parent_id":0} Now, I have an Angular view set up like this: <table id="example-datatables" class="table table ...

Activate an iframe upon changing the media query

I am currently working on a webpage where I have included an image within an iframe. I am looking to change the content displayed inside the iframe based on different media query breakpoints. For instance, if the viewport has a minimum width of 900px, I ...

Tips for creating a global variable by combining form input and an API request in React

Currently, I am in the process of constructing a simple login page for my React application. The way it works is that when a user submits a form to log in, if the entered username and password match what's stored in the database, an object is created ...

Angularjs - Navigating the Depths of OrderBy: Effective Strategies for Handling Complex Sorting Structures

I have a collection of Incidents (displayed as an array below) that I need to sort meticulously by State, Priority, and Start_date. Specifically, I want them ordered in the sequence of Initial > Ongoing > InReview > Resolved for State, then Priori ...

Unable to create a responsive layout because of the use of inline CSS with "margin"

Currently, I am working on a design for a webpage that must be responsive to large, medium, and small screens. To achieve this responsiveness, I am using Material UI Grid container and placing Chips inside it with the style={{ marginLeft: 60 }} from the se ...

What could be causing html-webpack-inline-source-plugin to prevent the page from refreshing after editing CSS files?

Currently, I am utilizing a plugin that embeds all CSS within the final HTML file. This method prevents the application from refreshing in development mode. Whenever I make edits to the CSS, the build process completes normally, but the styles do not updat ...

Toggle the visibility of a div depending on a value that is loaded on the page

I need help with a functionality that will hide or show the following div based on the value of <%= ItemCount%>. If the number is less than five, I want the div to be hidden. On the other hand, if it is above 5, then the div should be visible. Any ...