Issue with Sass @use failing to load partial

I'm currently facing an issue while trying to import a sass partial called _variables.scss into my main stylesheet named global.scss. Every time I compile the sass code, I encounter the following error message: Error: Undefined variable.

Here is the directory structure for reference:

app
└─public
  └─styles
    │  global.css (final output after sass compilation)
    └─sass
      └─_variables.scss
      └─global.scss

The content of _variables.scss looks like this:

$font-text: 'lato', sans-serif;

And here is the content of global.scss:

@use '_variables';

body {
  font-family: $font-text;
}

After referring to the Sass documentation, I learned that using @use instead of @import is recommended as they are phasing out the latter. However, despite making this change, the issue persists.

I would appreciate any insights on what I might be doing wrong here. Thank you!

Answer №1

If you're wondering why this feature isn't working for you, it could be because you're not using Dart Sass, which is the only implementation that currently supports it.

The documentation explicitly states:

Only Dart Sass currently supports @use. Users of other implementations must use the @import rule instead.

So, stick to using @import until full support for @use is available.

By the way, if you're interested in switching to Dart Sass, simply replace node-sass with sass and make sure to require it in your webpack configuration like so:

// ...
{
  loader: 'sass-loader',
  options: {
    implementation: require('sass')
  }
}
// ...

Answer №2

Short Answer

To access variables, functions, and mixins from another module, you can use < namespace >.< variable >, < namespace >.< function>(), or @include < namespace >.< mixin >(). By default, the namespace is typically the last component of the module’s URL.

This means utilizing variables.$font-text

global.scss

@use '_variables';

body {
  font-family: variables.$font-text;
}

SASS Documentation on Loading Members

Long Answer

The answer by @llobet involved using webpack, while @Jamie Marshall suggested using a gulp file. However, my approach was to utilize SASS with Node JS and I required a detailed guide to accomplish this task.

Here is my step-by-step guide on how I made @use work with Dart Sass and Node JS:

1. Create a New Node Project

npm init

2. Install Dart SASS

npm i sass

You can optionally install AutoPrefixer using

npm install postcss-cli autoprefixer

3. Configure NPM Script for Compiling SASS

In your Package.json, add the script compile:sass to compile SASS into CSS

"scripts": {
   "test": "echo \"Error: no test specified\" && exit 1",
   "compile:sass": "sass --watch scss:assets/css"
},

4. Create an SCSS Folder and Add style.scss Within It

The script compile:sass expects a folder named scss at the root of your project along with a .scss file inside that folder

Add the following content to your .scss file

@use '_variables';

body {
font-family: variables.$font-text;
}

5. Create Partial File _variables.scss

In the same directory as the SCSS folder, create a partial file named _variables.scss and insert the following code within it

$font-text: 'lato', sans-serif;

6. Compile SASS to CSS

Execute the following script in your project's root directory to compile SASS into CSS

npm run compile:sass

You should now find a folder named assets containing a subfolder named css, which houses your compiled CSS file.

Answer №3

This is a crucial update to the answer provided by @llobert. After extensive research, it has been discovered that node-sass relies on libsass, which is the C++ implementation of sass. The documentation for libsass repository states that this C++ implementation has been deprecated.

https://i.sstatic.net/QaK8Z.png

One might assume that switching to dart-sass is the solution, but even the documentation for dart-sass on npm is outdated:

https://i.sstatic.net/CXekc.png

Therefore, the necessary steps now include:

npm install sass

Furthermore, for those using gulp-sass like myself, it's important to note the information at the bottom of the gulp-sass npm page.

https://i.sstatic.net/eyH3A.png

Although the default settings remain the same, the underlying libsass library has become obsolete. Hence, the following line must be added to your gulp file:

var sass = require('gulp-sass');
sass.compiler = require('sass');

In conclusion, there have been significant changes in the SASS world that have occurred quietly yet decisively. I hope my recent experience helps save future developers time spent searching until the documentation catches up with these changes.

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

Complete Navigation Bar Expands Alongside Drop-down Menu

For my navbar, I am attempting to set a custom breakpoint at 1080px where it switches to a hamburger menu. However, when I click on a dropdown option, the entire navbar expands instead of just the dropdown area. @media (min-width: 1080px){ .navbar-expand- ...

Responsive navigation menu featuring dynamic dropdown menus using HTML and CSS

Looking to design a responsive HTML and CSS navigation menu with multiple dropdown menus? You're in the right place. I've scoured countless YouTube videos, but haven't quite found the perfect template for a two-part navigation menu that tic ...

My presentations are not functioning as expected, could there be a problem with my HTML, CSS, or JavaScript coding?

My website utilizes a Slideshow feature to display images for blog posts. Recently, I attempted to include multiple slideshows within an article, which unfortunately caused all of the slideshows to malfunction. As it stands now, when the code is executed, ...

Issue with maintaining image aspect ratio; unable to utilize background-image: cover property

If what I am attempting is not feasible, please inform me. I am currently working on displaying images of various sizes without distortion. While most images look good, those with a smaller width than the container of 285px are getting distorted. I am fin ...

CSS code for vertical navigation arrows to remain on both the left and right sides of the webpage

I'm struggling a bit with the CSS. I want to recreate the same effect as seen on . The left and right navigation arrows stay fixed vertically even when scrolling up or down the page. Does anyone have any code examples for that? ...

Can someone please provide guidance on how to focus on these boxes? (see image below)

My goal is to specifically target the boxes with white dots inside them. Each black box in this picture represents a blog post, even including the larger one at the top. I initially considered using the nth-child selector, but I'm not confident in how ...

What is the best way to create a smooth sliding effect using CSS?

Need help with creating a transition effect within a bordered div that reveals hidden content when a button is clicked? I'm looking to have the <a> tag displayed after clicking the button, with both the button and text sliding to the left. Here& ...

Struggling with pagination problems in Bootstrap 4 and looking to integrate QR code functionality into your blade template?

I am currently facing an issue with generating a batch of QR codes using the SimpleQR code generator in Laravel blade template on the fly for printing. The problem arises when a page break occurs, as it breaks the QR code. I attempted to use the break-insi ...

Identify and target a particular DIV based on the URL for customization

My webpage has a collection of frequently asked questions (FAQs) on a page called faq.html. Here is an example structure: <div id="faq1">...</div> <div id="faq2">...</div> I am looking to create a pulsating or highlighting effect ...

An interesting approach to utilizing toggle functionality in JQuery is by incorporating a feature that automatically closes the div when

Currently, I am utilizing JQuery's toggle function to slide a ul li element. However, my desired functionality is for the div to close if someone clicks outside of it (anywhere on the page) while it is in the toggle Down condition. Below, you'll ...

The jQuery onClick function functions effectively for the initial two clicks; however, it ceases to

I am currently experimenting with jQuery to dynamically load a specific div from another page on my server into a designated section on the existing page. While the code is successfully functioning for the first two clicks on the website, it fails to work ...

One-of-a-kind Version: "Utilizing CSS to Capitalize an

Imagine having the following strings. i and we. me and you. he and she. Is it possible to achieve the desired result using PURE CSS? I and We. Me and You. He and She. If capitalizing the text using text-transform: capitalize is used, it will yi ...

Exploring AngularJS: A Guide to Accessing Millisecond Time

Is there a way to add milliseconds in Time using AngularJS and its "Interval" option with 2 digits? Below is the code snippet, can someone guide me on how to achieve this? AngularJs Code var app = angular.module('myApp', []); app.controller(&ap ...

Create a layout consisting of two rows, each containing three blocks. Ensure that the layout is responsive by using only HTML and CSS, without relying

Hello there! I am looking to create a responsive layout without using bootstrap or any other framework..... I want to build it from scratch. https://i.stack.imgur.com/GAOkh.png I am aiming for responsive blocks that collapse into one column when the page ...

Items in the Vue.Draggable display can be arranged on both the left and right

Recently delving into Vue.js, I am utilizing Vuedraggable for item dragging in my project. Currently, the items in the draggable div are shown as Text 1 Text 2 Text 3 Text 4 Is there a way to rearrange them to display like this? Text 1 Text 2 Text 3 T ...

Having trouble aligning the unordered list with the input

Can anyone help me troubleshoot my CSS so that the list appears below the input field? Check out this example The nested menu in this example is positioned too far to the left and slightly too high. It should be aligned with the bottom of the containing ...

The issue with the <Button> component not properly linking in next.js

Having trouble with a button wrapped inside a custom Navbar component. The code snippet in question is: <NavBtn> <Link href="/contact" passHref> <Button> Contact Me </Button> </Link> & ...

Unable to display the absolute path of the image src in WebStorm

Hey there! I recently started learning HTML5 and have been using WebStorm on my trusty Macbook Air for about two weeks now. I've encountered a problem with image sourcing that has left me puzzled. Here's my question: Why is it that images from ...

What is the reason for parent rows not stretching fully across the width of the page?

I am working on creating a simple table in Vue.js with Bootstrap. When the arrow is clicked, the child row is displayed, and I want it to appear below the parent row. I achieved this by setting display:flexbox; flex-direction:column; Here is the HTML tabl ...

React not displaying wrapped div

I am facing an issue with my render() function where the outer div is not rendering, but the inner ReactComponent is displaying. Here is a snippet of my code: return( <div style={{background: "black"}}> <[ReactComponent]> ...