Close
Type at least 1 character to search
Back to top

Watch and Compile SASS with Node.js, the easy way

Sass is hands-down my favorite CSS pre-processor and it is commonly associated with writing a clean, reusable, and modular CSS. There are many SASS compiling tools available out there – paid as well as free to use. Personally, I have been using Koala for a long time as it is a great tool and it’s free.

Having said that, using Node.js and your Terminal to compile your SASS is definitely the most convenient way to do it.

Naturally, it can be a bit confusing meeting Node.js for designers who are just about to learn some coding, but it is quite easy to start. And once you get used to having such great power of npm packages under your fingertips, you will never look back.

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.

Macintosh:~You$ cd Users/You/NewProject 

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.

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

Now, 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-test-compiler",
"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, below the test command, as it’s shown:

"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.
  • node-sass: Refers to the node-sass package.
  • –watch: An optional flag which means “watch all .scss files in the scss/ folder and recompile them every time there’s a change.”
  • scss: The folder name where we put all our .scss files.
  • -o css: The output folder for our compiled CSS.
When we run this script, it will watch every .scss file in the scss/ folder, and save the compiled css in css/ folder every time we change (and save) a .scss file.

5. Run the Script

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

npm run scss 

And there it is. If you have followed all the steps successfully, watching and compiling SASS with Node.js should be up and running.