Watch and Compile Sass in Five Quick Steps

Sass is perhaps the most popular of the CSS pre-processors; for years it’s helped us write clean, reusable and modular CSS. In this quick tutorial, I’ll cut straight to the stuff that matters and explain how to compile Sass into CSS using the command line.

Watch and Compile Sass in Five Quick Steps

Watch and Compile Sass in Five Quick Steps

1. Install Node.js

To compile Sass via the command line, first we need to install node.js. Download it from the official website nodejs.org, open the package and follow the wizard.

2. Initialize NPM

NPM is the Node Package Manager for JavaScript. NPM makes it easy to install and uninstall third party packages. To initialize a Sass project with NPM, open your terminal and CD (change directory) to your project folder.

Watch and Compile Sass in Five Quick Steps

Once in the correct folder, run the command npm init. You will be prompted to answer several questions about the project, after which NPM will generate a package.json file in your folder.

Watch and Compile Sass in Five Quick Steps

3. Install Node-Sass

Node-sass is an NPM package that compiles Sass to CSS (which it does very quickly too). To install node-sass run the following command in your terminal: npm install node-sass

4. Write Node-sass Command

Everything is ready to write a small script in order to compile Sass. Open the package.json file in a code editor. You will see something like this:

{
  "name": "sass-tutorial",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

In the scripts section add an scss command, under the test command, as it’s shown below:

"scripts": {
  "test": "echo "Error: no test specified" && exit 1",
  "scss": "node-sass -watch scss -o css"
}

Let’s go through this line word by word.

  1. node-sass: Refers to the node-sass package.
  2. -watch: An optional flag which means “watch all .scss files in the scss/ folder and recompile them every time there’s a change.”
  3. scss: The folder name where we put all our .scss files.
  4. -o css: The output folder for our compiled CSS.

When we run this script it will watch every .scss file in the scss/ folder, then save the compiled css in css/ folder every time we change a .scss file.

5. Run the Script

To execute our one-line script, we need to run the following command in the terminal: npm run scss

And voila! We are watching and compiling SASS.

Watch and Compile Sass in Five Quick Steps

Sass Quick Tips

Stay tuned for more Sass quick tips; there’s a whole collection on the way!