Create-react-app: When using sass, how to import sass variables?

Created on 22 Feb 2017  Â·  13Comments  Â·  Source: facebook/create-react-app

Hi, first of all thanks for such a great react development setup & build automation tool.

When using create-react-app with node-sass. How to expose sass variables to my project? Can't get my head around..

My folder structure for the reference:

└───src
    ├───app
    │    └───variables.scss // here are all my vars
    ├───components
    │   └───Header // there is Header.scss in this folder, where I need to use my variables
    ├───helpers
    ├───public
    └───utils

Most helpful comment

Couldn't you also @import "../../variables.scss"; or equivalent from any component's Sass file that needs them?

All 13 comments

one way to do it is just import any scss file from your root index.scss

so rather than placing import './Header.css'; in Header.js
You do

@import "./app/variables.scss";
@import "./components/Header/Header.scss";

in index.scss

and import './index.css' in index.js

@viankakrisna thanks, this works! :)

Couldn't you also @import "../../variables.scss"; or equivalent from any component's Sass file that needs them?

As always, @gaearon comments is an eye opener đź‘Ť @grundmanise please use this instead of my suggestion. It's more inline with component based structure.

@gaearon do you have a suggestion to use _absolute paths_ for sass imports? For example, when trying to import src/shared/variables.scss from a deeply nested component, there are a lot of ../../../. Related: https://github.com/facebookincubator/create-react-app/pull/1712 (for importing JS files)

No, I don’t know how to solve this aside from moving them into an npm package and then using ~packagename/file.css.

You can use sass-resources-loader to load global variables into every sass file before output. In addition to the other style loaders for scss files in your webpack config:
{ loader: 'sass-resources-loader', options: { resources: './client/scss/resources/**/*.scss' }}
This will load all files in the client/scss/resources directory and subdirectories into scope for all processed scss files. As long as those files don't directly generate css, you won't get any repeated code in your output css.

Getting hit with an error when trying to use @import inside of a SASS file in a create-react-app app. SASS has been set up as the docs suggest. Here is a link to the offending file.

Error Message

{
  "status": 1,
  "file": "/Users/ben/Desktop/Work/code/indio-form-builder/src/components/Tab/tab.scss",
  "line": 3,
  "column": 1,
  "message": "media query expression must begin with '('",
  "formatted": "Error: media query expression must begin with '('\n        on line 3 of src/components/Tab/tab.scss\n>> .active a {\n   ^\n"
}

My code

@import "./constants.scss"

.active a {
  background-color: $tabColor;
}

You can also load the styles into a root component and pass them down as props, but maybe that is just a React Native strategy, I'm not certain.

I will just mention it anyway for the more savage yet creative individuals. It can facilitate theming your app by swapping out variables at the root level, perhaps based on user-preferences.

@woniesong92 npm package seems to work for me, also have very deeply nested files

@bengrunfeld you're missing a semicolon on your import. Damn things.

Any new suggestions?
I'm using Sass in a Create React App project. I have a master.scss file with global var's, I would like to use those var's globally... (I don't want to @import "master" in every scss file ), Any other ideas how to make it globally available ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JimmyLv picture JimmyLv  Â·  3Comments

alleroux picture alleroux  Â·  3Comments

Aranir picture Aranir  Â·  3Comments

wereHamster picture wereHamster  Â·  3Comments

stopachka picture stopachka  Â·  3Comments