1


I need to compile SASS and SCSS Strings to CSS Strings for a project. Therefore I´m using the node package node-sass. Converting SCSS works fine, but SASS doesn't.

Example code for SASS:

'use strict';

var sass = require('node-sass');

var dataTemp = 'body{background:blue; a{color:black;}}';

var output = sass.renderSync({
  data: dataTemp
});

console.log(output.css.toString());

The problem comes, when I try to convert something like this, which should be valid .sass syntax

'use strict';

var sass = require('node-sass');

var dataTemp = '.red\n color: red';

var output = sass.renderSync({
  data: dataTemp
});

console.log(output.css.toString());

Console output:

Error: Invalid CSS after ".": expected 1 selector or at-rule, was ".red color: red "

So obviously node-sass can't compile .sass code that way. The syntax of node-sass is:

var result = sass.renderSync({
  data: scss_content
  [, options..]
});

Inside the object you can define options and even functions. Maybe is there a way to compile the data in such a function?

Any other solutions or recommendations for other packages?

2
  • You haven't told us why this doesn't work? You also have a typo in outpu.css Commented Apr 27, 2016 at 9:41
  • Thanks for the feedback! :) Commented Apr 27, 2016 at 13:52

2 Answers 2

5

Well by adding an option named 'indentedSyntax' and setting it to 'true', node-sass can compile .sass code as well. Sorry, should have read the documentation more precisely. Thanks for helping!

'use strict';

var sass = require('node-sass');

var dataTemp = '.red\n color: red';

var output = sass.renderSync({
  data: dataTemp,
  indentedSyntax: true,
  outputStyle : 'compressed'
});

console.log(output.css.toString());

Log: ".red{color:red}"

Sign up to request clarification or add additional context in comments.

Comments

2

Your SCSS is invalid - you need to provide the {} around your properties:

'use strict';

var sass = require('node-sass');

var dataTemp = '.red {color: red}';

var output = sass.renderSync({
  data: dataTemp
});

console.log(output.css.toString());

You can validate your SASS/SCSS using an online tool such as SassMeister

3 Comments

Right, the normal SCSS Syntax works fine, but the SASS Syntax, which I need aswell doesn't. Had a typo there, but '.red\n color: red' should be vaild .sass Syntax.
I'm not sure that's correct. See the update to my post above - using the validator, your original value is invalid, but the value I have suggested is valid.
Unfortunately I´m not that familiar with SASS/SCSS, but I think I need the Syntax discribed in link, where you can witch between SCSS and SASS Syntax. Therefore I thought, that ".red\n color: red" should be valid?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.