question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

create-react-app 2.0, how to use absolute path import in sass/scss files?

See original GitHub issue

Is this a bug report?

No

The problem

I am using creacte-react-app 2.0.0-next.66cc7a90, which already has support for Sass/Scss files

however with default relative import in Sass/Scss files, i have to use code like @import "../../../../../styles/variables/_variables.scss";

instead I want to use absolute import so i can import with code @import "styles/variables/_variables.scss";

I know one way to acheve so is to update options

{
   loader: "sass-loader",
   options: {
        includePaths: ["absolute/path/a", "absolute/path/b"]
 }

but I want to do it without ejecting, how can i do it? using react-app-rewired?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:22 (4 by maintainers)

github_iconTop GitHub Comments

15reactions
fbarbarecommented, Oct 10, 2018

@Timer This is an issue when using SCSS files from NPM packages where they also get SCSS files from other packages.

A very good example is @material: You will need to import their SCSS like so:

@import '~@material/button/mdc-button';

But inside their SCSS file they have:

@import "@material/ripple/common";
@import "@material/ripple/mixins";
@import "./mixins";
@import "./variables";

So the build breaks as they do not have the ~. On their side, it makes sense as that’s how SASS works. But sass-loader makes it complex as they need the ~ to go look inside the node_modules.

For CRA, including this option fixes this problem (tested on my local):

includePaths: ["node_modules"]
6reactions
justocommented, Aug 10, 2018

I’m successfully using absolute imports with ~ and NODE_ENV set in .env

/.env:

NODE_PATH=src/

given that /src/styles/variables.scss exists, in any .scss file:

@import ~styles/variables`

works fine with in v2.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

create-react-app 2.0, how to use absolute path import in sass ...
Assuming styles/abstracts/_variables.scss exists under src , you could do the following: SASS_PATH=src yarn start.
Read more >
Adding a Sass Stylesheet - Create React App
To use imports relative to a path you specify, you can add a .env file at the project root with the path specified...
Read more >
Absolute imports with Create React App | by David Gilbertson
With the release of Create React App 3, we now have the ability to use absolute import paths, without ejecting. Hallelujah.
Read more >
Absolute imports in Create React App - DEV Community ‍ ‍
Thanks a lot! Have you any tips in case if I want organize paths for styles structure? What should I do with imports...
Read more >
How to use Sass and CSS Modules with create-react-app
Up until the release of create-react-app v2, if you wanted to include Sass or CSS Modules in your project, you would have to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found