Compiling sass with gulp using NodeJS

Posted By : Arun Kumar | 02-Feb-2016

Gulp : Gulp is a task runner which can automate the many build tasks such as css preprocessing,minification,live reloading and many more.

Pre-requisite : Node.js must be installed on your machine.

Note: We will be installing gulp and express in the project directory ,not globally for this tutorial.

Step 1: Create a project directory and create a package.json file in it.

            package.json can also be created automatically by using the command prompt by the command "npm init" .
 Step 2; Install express.

            In the command prompt type in "npm install --save-dev express". This will install express framework with in a node_modules folder inside your project directory.

            We will be using express to run the server for this tutorial.

Step 3: Install gulp

            In the command prompt type in "npm install --save-dev gulp" to install gulp in the project directory.

Step 4: Install sass compiler.

            In the command prompt type in "npm install --save-dev gulp-sass" to install the sass compiler.

Step 5:Now create two directories one for sass and the other one for css.We are going to name them "sass" and "css".

Step 6: Create a Gulpfile.js in the root directory of the project and paste in the following code.

var gulp = require('gulp');

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

gulp.task('express', function() {  

var express = require('express');  

var app = express();  

app.use(express.static(__dirname));  

app.listen(4000, '0.0.0.0'); });

gulp.task('styles', function() {
    gulp.src('sass/**/*.scss')
        .pipe(sass().on('error', sass.logError))
        .pipe(gulp.dest('./css/'));
});

gulp.task('default', ['styles', 'express'], function() {

});

 

 

Step 7: Create a index.html file for a webpreview and paste in the following html code.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title></title>
</head>
<body>
  Hello World!
</body>
</html>

 

Step 8: Now run command: gulp from the command prompt and this will run your server.

Start typing your sass code inside a sass file in the sass folder and it will get compiled to a css file inside the css folder.

 

About Author

Author Image
Arun Kumar

Arun is a creative UI Developer

Request for Proposal

Name is required

Comment is required

Sending message..