Socket
Socket
Sign inDemoInstall

node-sass

Package Overview
Dependencies
1
Maintainers
1
Versions
148
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    node-sass

wrapper around libsass


Version published
Weekly downloads
1.7M
decreased by-1.1%
Maintainers
1
Created
Weekly downloads
 

Package description

What is node-sass?

The node-sass npm package is a library that allows you to natively compile .scss files to CSS at incredible speed and automatically via a connect middleware. It provides a binding for Node.js to the Sass engine, which is written in C++ and allows for the translation of SCSS or SASS syntax into standard CSS that browsers can understand.

What are node-sass's main functionalities?

Compiling SCSS to CSS

This feature allows you to compile .scss files into .css files. The 'render' method takes an options object and a callback function. The options object specifies the input and output paths for the SCSS and CSS files, respectively. The callback is invoked after the compilation process, and you can handle the result or error accordingly.

const sass = require('node-sass');
sass.render({
  file: 'path/to/input.scss',
  outFile: 'path/to/output.css'
}, 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('path/to/output.css', result.css, function(err){
      if(!err){
        //file written on disk
      }
    });
  }
});

Watching files or directories

This feature allows you to watch .scss files or directories for changes and automatically recompile them to CSS when a change is detected. The example uses 'chokidar', an external library for watching files, to listen for changes on the specified SCSS file and then uses node-sass to compile the file to CSS.

const sass = require('node-sass');
const chokidar = require('chokidar');

chokidar.watch('path/to/input.scss').on('change', () => {
  sass.render({
    file: 'path/to/input.scss',
    outFile: 'path/to/output.css'
  }, function(error, result) {
    if (!error) {
      fs.writeFile('path/to/output.css', result.css, function(err){
        if(!err){
          console.log('SCSS file updated.');
        }
      });
    }
  });
});

Command Line Interface (CLI) usage

node-sass provides a CLI for compiling SCSS files to CSS directly from the command line. In this example, the '--output-style' option is used to specify the CSS output format (compressed in this case), '-o' is used to define the output directory for the compiled CSS, and the last argument is the input directory containing the SCSS files.

node-sass --output-style compressed -o dist/css src/scss

Other packages similar to node-sass

Readme

Source

##node-sass

Node bindings to libsass

Install

npm install

Usage

var sass = require('sass');
sass.render('body{background:blue; a{color:black;}}', function(err, css){
  console.log(css)
});

Connect/Express middleware

Recompile .scss files automatically for connect and express based http servers

var server = connect.createServer(
  sass.middleware({
      src: __dirname
    , dest: __dirname + '/public'
    , debug: true
  }),
  connect.static(__dirname + '/public')
);

Heavily inspired by https://github.com/LearnBoost/stylus

TODO

  • sass compression options
  • publish npm
  • file context?
  • folder context?

FAQs

Last updated on 12 Jun 2012

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc