Advertisement
  1. Web Design
  2. HTML/CSS
  3. CSS

Watch and Compile Sass in Five Quick Steps

Scroll to top
Final product imageFinal product imageFinal product image
What You'll Be Creating

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.

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.

Navigating to SASS-tutorial folderNavigating to SASS-tutorial folderNavigating to SASS-tutorial folder
Navigating to Sass project folder

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.

packagejsonpackagejsonpackagejson

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:

1
{
2
  "name": "sass-tutorial",
3
  "version": "1.0.0",
4
  "description": "",
5
  "main": "index.js",
6
  "scripts": {
7
    "test": "echo \"Error: no test specified\" && exit 1"
8
  },
9
  "author": "",
10
  "license": "ISC"
11
}

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

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

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.

Sass Quick Tips

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

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.