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.

Sass `compileString` unable to import stylesheets

See original GitHub issue

Node version: 18.4.0 Sass version: 1.54.0 OS: macOS 12.4 (M1/ARM)

I am unable to use the compileString() method to process CSS that imports other stylesheets via @use or @forward. I have tried absolute paths and numerous relative path variations. No luck. FWIW, the same text contained in a file can be processed using the compile() method and the Sass CLI without issue.

Steps to reproduce:

Create the following three files:

// File: test.scss

@use "test-import";

.my-class {
    color: red;
}
// File: test-import.scss

.my-import {
    color: blue;
}
// File: test.js

const fs = require('fs');
const path = require('path');
const sass = require('sass');

const srcPath = path.resolve(__dirname, 'test.scss');
const srcText = fs.readFileSync(srcPath, 'utf8');

try {
    const result = sass.compile(srcPath);
    console.log('sass.compile() success');
    console.log(result);
}
catch(err) {
    console.error('sass.compile() fail');
    throw(err);
}

try {
    const test2 = sass.compileString(srcText);
    console.log('sass.compileString() success');
    console.log(result);
}
catch(err) {
    console.error('sass.compileString() fail');
    throw(err);
}

Run the test.js file:

node ./test.js

The sass.compile() test will complete successfully and display the results object to the console. The CSS generated is as expected:

.my-import {
  color: blue;
}

.my-class {
  color: red;
}

The sass.compileString() test will fail and display the following in the console:

sass.compileString() fail
/Users/john/Dev/project-name/test.js:27
    throw(err);
    ^

sass.Exception [Error]: Can't find stylesheet to import.
  ╷
1 │ @use "test-import";
  │ ^^^^^^^^^^^^^^^^^^
  ╵
  - 1:1  root stylesheet
    at Object.wrapException (/Users/john/Dev/project-name/node_modules/sass/sass.dart.js:1246:17)
    at _EvaluateVisitor1._evaluate0$_loadStylesheet$4$baseUrl$forImport (/Users/john/Dev/project-name/node_modules/sass/sass.dart.js:80868:19)
    at _EvaluateVisitor1._evaluate0$_loadStylesheet$3$baseUrl (/Users/john/Dev/project-name/node_modules/sass/sass.dart.js:80894:19)
    at _EvaluateVisitor__loadModule_closure4.call$0 (/Users/john/Dev/project-name/node_modules/sass/sass.dart.js:82231:21)
    at _EvaluateVisitor1._evaluate0$_withStackFrame$1$3 (/Users/john/Dev/project-name/node_modules/sass/sass.dart.js:81853:25)
    at _EvaluateVisitor1._evaluate0$_withStackFrame$3 (/Users/john/Dev/project-name/node_modules/sass/sass.dart.js:81859:19)
    at _EvaluateVisitor1._evaluate0$_loadModule$7$baseUrl$configuration$namesInErrors (/Users/john/Dev/project-name/node_modules/sass/sass.dart.js:80323:13)
    at _EvaluateVisitor1._evaluate0$_loadModule$5$configuration (/Users/john/Dev/project-name/node_modules/sass/sass.dart.js:80326:19)
    at _EvaluateVisitor1.visitUseRule$1 (/Users/john/Dev/project-name/node_modules/sass/sass.dart.js:81143:13)
    at UseRule0.accept$1$1 (/Users/john/Dev/project-name/node_modules/sass/sass.dart.js:98288:22)

Note that I also tried variations on the import path but did not have any success:

  • @use "test-import.scss";
  • @use "./test-import";
  • @use "./test-import.scss";
  • @use "/Users/john/Dev/project-name/test-import";
  • @use "/Users/john/Dev/project-name/test-import.scss";

I’d love to see this resolved as it would make unit testing Sass mixins and functions much simpler.

Thanks!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jhildenbiddlecommented, Aug 3, 2022

Thanks @nex3 and @jathak.

If you don’t mind me making a suggestion, I think making it more clear that compileString accepts a string or an object containing options would be helpful.

The Usage section states:

CleanShot 2022-08-02 at 20 58 00@2x

Changing this to something like the follow would have made this much more obvious:

It is much more clear that an options object can be passed if a user clicks the compileString link:

CleanShot 2022-08-02 at 20 56 05@2x

Just my $0.02. If everyone else figured it out except me it wouldn’t be the first time. 😄

Thanks again!

0reactions
jhildenbiddlecommented, Oct 14, 2022

@nex3

Yep. My bad. Thankfully @Goodwine didn’t repeat my mistake in the doc updates. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

SassError: Can't find stylesheet to import when it contains ...
sass -loader throws following error when @import 'ed Sass file contain @forward statement with _index file. (See reproducible steps below).
Read more >
Sass "Error: Can't find stylesheet to import." - Stack Overflow
I'm trying to follow the workflow described in https://getbootstrap.com/docs/4.1/getting-started/theming/ to import Bootstrap's SCSS source ...
Read more >
sass | JS API
Synchronously compiles a stylesheet whose contents is source to CSS. If it succeeds it returns a CompileResult, and if it fails it throws...
Read more >
Options | JS API - Sass
Stylesheets that are imported relative to the entrypoint are not considered dependencies. This is useful for silencing deprecation warnings that you can't fix ......
Read more >
Importer | JS API - Sass
As above, if the importer finds a stylesheet at more than one of these URLs, it should throw an exception indicating that the...
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