2

I'm trying to compile my scss file into css file using node-sass , I already know how to do this with Command line. I have this npm-script In my package.json file :

"scripts": {
"scss": "node-sass --watch fileName/scss -o FileName/css"
}

and when I run :

npm run scss

it works fine. What I want to know is how to use (from the node-sass project page) :

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

sass.render({
  file: scss_filename,
  [, options..]
}, function(err, result) { /*...*/ });

I try this in my app.js :

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

sass.render({
  file: __dirname + '/scss/style.scss',
  outFile: __dirname + '/css/style.css',
}); 

but it's not working, I have little experience with this and I can use some help.

1 Answer 1

3

within the same documentation you linked to

sass.render({
    ...
    outFile: yourPathTotheFile,
  }, function(error, result) { // node-style callback from v3.0.0 onwards
    if(!error){
      // No errors during the compilation, write this result on the disk
      fs.writeFile(yourPathTotheFile, result.css, function(err){
        if(!err){
          //file written on disk
        }
      });
    }
  });
});

the sass.render() doesn't write to the file, it only passes the result to a callback. You need to write it yourself using fs as in the example above.

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

2 Comments

What the fs is here? I get an error that fs is not defined. What it should refer to?
node file system add const fs = require('fs');

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.