Problem with referencing image URLs in Sass file of Create React App generated project

My folder structure looks like this:

-- src
---- components
------ App.js
------ App.scss
---- img
------ icon-gear.png
---- styles
------ base.scss

Let's dive into the relevant parts of my component file (App.js):

import './App.scss';

class App extends Component {

Now, let's take a look at the pertinent sections of my Sass file (App.scss):

@import '../styles/base.scss';

.App {
  .App-btnSettings {
    background-image: url('../img/icon-gear.png');
  }
}

I'm having trouble getting the path for the icon-gear image right.

When I use ../img/icon-gear.png, it throws an error:

Module not found: Can't resolve '../img/icon-gear.png' in '/Users/rob/develop/openbazaar/openbazaar-web/src/styles/components'

And when I try ../../img/icon-gear.png, another error pops up:

./src/components/App.scss... You attempted to import ../../img/icon-gear.png which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.

I'm puzzled about what the root path for the image should be relative to. I initially thought it should be src/components since that's where the sass file is and where I import the stylesheet (@import '../styles/base.scss'). However, that doesn't work (leading to the first error mentioned).

It seems there's inconsistency with the base path between the two error messages.

Answer №1

It seems like there is an issue with your folder structure.

-- src
---- components
------ App.js
------ App.scss
-- img
---- icon-gear.png
-- styles
---- base.scss

Should be corrected to

-- src
---- components
------ App.js
------ App.scss
---- img
------ icon-gear.png
---- styles
------ base.scss

After making this change, Webpack should properly recognize relative paths.

Additionally, confirm that node-sass is installed at the top level using the following command:

npm install --save-dev node-sass

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

Is there a way to update or modify the media queries set in the Bootstrap CSS file

I don't want the media queries defined in the Bootstrap CSS to override my custom CSS when the user resizes their window. Here is my HTML code: <div class="row"> <div class="col-xs-6"> <dl class="dl-horizontal dl-horizontal-info custo ...

Choose all immediate parent items on the list in the mmenu

Currently, I am working on a project and looking to incorporate the jQuery plugin mmenu (). Despite making some customizations to suit my requirements, I am facing a dilemma. In mmenu, clicking on a list entry navigates to the specified href and marks the ...

Top div click causes bottom div to slide

I'm currently working on a project that involves using CSS and jQuery, but I've encountered an issue that I haven't been able to solve. https://i.sstatic.net/CSovs.png Here is the demo link: . When the second div is clicked, why does the t ...

PnP is not supported by NPM

Trying to incorporate pnpm with create-react-app v2, I utilized the command below: npx create-react-app my-app --use-pnp Despite this, a warning popped up stating: NPM doesn't support PnP. Falling back to the regular installs. What steps should be ...

Embed an iframe that occupies the entire page, all without the use of

I am facing a challenge with an iframe on my webpage, as it appears in letterbox shape. My goal is to make it fill the page vertically while maintaining the horizontal size at 60% with sidebars on the left and right. Currently, I have achieved this in Chr ...

The updated Capybara now supports the CSS 'contains' selector

Before, my specifications had these lines: within "h3:contains('FooBar text') + dl" do page.should have_content 'FizzBuzz' end (within the definition list following a header that contains specific text) I recently updated capybara- ...

The Bootstrap row does not display divs in a stacked formation when viewed on smaller screens

My layout is using bootstrap rows for two divs. I want them to be side by side on large devices, but stacked on small devices. However, Bootstrap is not stacking them on small devices and they are still both sitting at 50%. The specific divs I am talking ...

The alignment of 5 boxes is off, failing to position them in the center of the page

Just dipping my toes into the world of programming. I managed to line up 5 boxes horizontally, but now I want to center them. The challenge is also making sure the boxes remain responsive. I've been scouring for ways to center a div, and all I come up ...

What is the best way to add style to the title attribute of a dropdown menu item?

In my webform project, I have implemented a dropdown menu with custom tooltips for the list items. These tooltips are different from the display field and value field, which can be considered as the third column. To bind the tooltips to the list items usin ...

Tips for maintaining the nested collapsible table row within the main table row in a React MUI table

I am working on implementing a Material UI (MUI) table that includes collapsible table rows. The challenge I am facing is maintaining consistent border and background colors across all the rows, even when some are collapsed. Currently, the separate rows ar ...

Clipped Words & Silhouettes

I'm having trouble ensuring the text in this particular example displays correctly. I am experiencing challenges with the clipping of text and shadows on certain letters, and I'm struggling to identify the root cause as well as the solution. In ...

I'm having trouble getting the "flex direction: row" property to work within a table row

First, I applied flex to the table > tr. Next, I configured the flex-direction to be row. table { width: 100%; } tr { display: flex; flex-direction: row; flex-wrap: wrap; overflow: hidden; width:100%; } td:nth-child(1) { flex: 0 0 20%; ...

Animate owlCarousel slide motion in reverse when using the Prev and Next buttons

The default functionality of owlCarousel is to move to the right when clicking Next and to the left when clicking Prev. However, after applying animations, both directions always move in the same direction. Check out this sample on jsfiddle to see the iss ...

Unable to locate the React native Android directory

Check out this image: I'm having trouble locating the Android File in my React Native project folder. I need to convert my project into a .APK file, but without the Android folder, I can't accomplish it. Can anyone offer assistance? I attempted ...

Scrolling Content CSS Sticky iPhone Mockup

Can someone assist me with building an iPhone Mockup with scrolling Content? I want the Mockup to be positioned in the middle of a web page. Specifically, when I scroll down and the iPhone is visible, I would like it to remain sticky so that only the conte ...

How can we use Jquery to add animation effects to our styling

I am a beginner with JQuery and struggling to make the code below work correctly. $('.announcement_panel_button').click(function(e){ $('#site_logo').animate({ 'margin-top': '5px' }, 5000); e.pre ...

Setting overflow sizes manually for parent containers when child elements are transformed using CSS can be done by following these steps

It has come to my understanding that CSS transforms do not impact the actual size of an element, rather its visual representation. There have been discussions on this topic: CSS Scale transform on child not affecting parent size CSS transform: scale does ...

When the last character in the route is a forward slash, the Express server will load the HTML page with CSS and JavaScript. Conversely, if the last route character

On my second html page model.html, I noticed the following issue: When I include a '/' at the end of my route address in the browser like this: http://localhost:3002/home/model/, the correct html page is loaded, but the css/js files do not load. ...

How to make the second div start on a new row when the first div exceeds its space in CSS

I am trying to figure out how to have two divs side by side with equal width. However, if the text in the first div wraps onto a second line, I want the second div to move below the first one and both divs should double in width. Can CSS handle this type o ...

Strange rendering in CSS JQuery Datatable

I am encountering an issue with a DataTable from datatables.net. Each row can be selected by clicking on a checkbox, which triggers a click event. In response to the event, I add or remove a CSS class to change the appearance of the selected rows. $(".cbD ...