Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to use sass modules in create-react-app with the new @use syntax but receiving an error

Tags:

sass

I've npm installed the latest node-sass, and scss files work fine until I use @use. I have a _tokens.scss file in /shared/tokens/ folder. Within _tokens.scss I have:

$colorwhite: #ffffff;

In my root folder, my App.scss looks like this:

@use "shared/tokens/tokens";

.App-header {
  color: tokens.$colorwhite;
}

But I am getting this error:

./src/App.scss (./node_modules/css-loader/dist/cjs.js??ref--6-oneOf-5-1!./node_modules/postcss-loader/src??postcss!./node_modules/resolve-url-loader??ref--6-oneOf-5-3!./node_modules/sass-loader/dist/cjs.js??ref--6-oneOf-5-4!./src/App.scss)
SassError: Invalid CSS after "  color: tokens": expected expression (e.g. 1px, bold), was ".$colorwhite;"
        on line 26 of /Users/xxx/src/App.scss
>>   color: tokens.$colorwhite;

Any ideas?

Edit: it works fine with @import. I've also tried @use... as * but no-go.

like image 669
herman Avatar asked Feb 21 '20 16:02

herman


People also ask

How to install Sass in react?

After you have setup your application with create-react-app (e.g. create-react-app my-app ), you can install the Sass support with npm install node-sass --save for create-react-app. You don't need to change any other configuration. Now, let's try out how Sass in React works.

How to add node-Sass module to react project?

Now install the node-sass module to your react project. 3. In your project src directory create an app.scss file. 4. Now add the below code in your app.js file 5. Open your app.scss file and add below sass code there.

Is it possible to use Sass and CSS modules in create-react-app?

Not anymore. The team at Facebook released create-react-app v2 and among the many improvements, they gave us the ability to use Sass and CSS Modules right out of the box, and in this tutorial you will learn how simple it is to include them in your projects using create-react-app. Let’s create a simple project to demonstrate this.

Is it possible to use CSS and SCSS modules with react?

I would suggest you check your version of React first. If you are using a version of React < 16.8 then you would have to eject and configure your webpack in order to use css and scss modules. Show activity on this post.


2 Answers

The @use rule is currently only supported by Dart Sass. You should use @import instead.

like image 107
Arkellys Avatar answered Sep 19 '22 06:09

Arkellys


You could switch to Dart Sass by taking advantage of package-aliasing. I guess this will not work under yarn.

First you remove the current node-sass pkg, then run:

npm install node-sass@npm:sass.

And you are fit to use the full features from sass with some remarks though.

  • Check the behavioral diffs here.
  • The performance measurement indicates that dart-sass is slower than node-sass.

It's all up to you and this turns to be a valid option here, if you really want to use these features provided by Sass.

like image 45
Arp Avatar answered Sep 22 '22 06:09

Arp