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.

scss @import fail

See original GitHub issue

Hi, I am using css-modules-require-hook on my koa.js server. I do server side rendering of react components. It worked fine when just using css but now we want to switch to Sass in order to use variables.

I am using the following code to compile scss on runtime:

// css modules
const cssRequireHook = require('css-modules-require-hook');
const sass = require('node-sass');
cssRequireHook({
    generateScopedName: '[name]__[local]___[hash:base64:5]',
    extensions: [ '.scss', '.css' ],
    preprocessCss: data => sass.renderSync({ data }).css
});

It works, but as soon as I try to @importany other .scss file it breaks

@import '../../styles/variables';

.myclass {
    background: $color-white;
    clear: both;
}

in variables.scss

$color-white: #000;

the error I get:

/Users/tmaximini/data/frontend-node/node_modules/node-sass/lib/index.js:424
  throw util._extend(new Error(), JSON.parse(result.error));
  ^

Error: File to import not found or unreadable: ../../styles/variables

the path is definitely correct, and webpack handles it fine on client. I don’t use webpack on my server code so I hoped that the require-hook can somehow resolve these @imports.

Any idea how to achieve this? thanks

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
sullenorcommented, Mar 10, 2016

Presumably sass resolves paths from the working directory. I’d like to suggest you to try this:

cssRequireHook({
    generateScopedName: '[name]__[local]___[hash:base64:5]',
    extensions: [ '.scss', '.css' ],
    preprocessCss: (data, filename) =>
      sass.renderSync({
        data,
        file: filename,
      }).css,
});

from: https://github.com/sass/node-sass#file

3reactions
tmaximinicommented, Mar 10, 2016

seems to work when I tell the sass.renderSync function to include my styles path:

cssRequireHook({
    generateScopedName: '[name]__[local]___[hash:base64:5]',
    extensions: [ '.scss', '.css' ],
    preprocessCss: data => sass.renderSync({
        data,
        includePaths: [path.resolve(__dirname, '../../client/styles')]
    }).css
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack throwing error on SCSS @import SCSS
I managed to reproduce the error and it seems to come from: include: [ path.resolve(__dirname, 'styles') ],. The path is the issue cause...
Read more >
SCSS imports fail · Issue #6058 - GitHub
I had a working webpack setup for my project. But out of a sudden, the loader can't import any of the following modules:...
Read more >
Sass: @import
When Sass imports a file, that file is evaluated as though its contents appeared directly in place of the @import . Any mixins,...
Read more >
Error on importing scss files. | React - EJ 2 Forums - Syncfusion
import the scss files from the node modules package in your App. · install the node-sass package using the command “npm install node-sass...
Read more >
scss @import fail - Laracasts
I'm a newbie here so please let me know if I've overlooked something obvious. I'm trying to employ the general practice of keeping...
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