1

I am unable to compile scss files to css once the application is run using npm start. Is there a way to live reload a sass to css like ts are compiled to js by utility tsc:w.So I have to stop the build and then do gulp watch or node-sass command to compile scss to css and then build solution again using npm start. I tried below approaches but none worked. Here is the code in package.json:

Using gulp watch:

"start": "concurrently \"npm run tsc:w\" \"npm run lite\" \"gulp\" ", 
//also tried gulp watch and that did not work too.
"start": "concurrently \"npm run tsc:w\" \"npm run lite\" \"gulp watch\" ",

here is my gulppfile.js in the root directory:

'use strict';
var gulp = require('gulp');
var sass = require('gulp-sass');
gulp.task('sass', function () {
  gulp.src('./sass/**/*.scss')
    .pipe(sass().on('error', sass.logError))
    .pipe(gulp.dest('./css'));
});
gulp.task('watch', function () {
  gulp.watch('./sass/**/*.scss', ['sass']);
});

Using node-sass command:

"start": "concurrently \"npm run tsc:w\" \"npm run lite\" \"npm run sass\" ",             
"sass": "node-sass -w app/sass/ -o app/css/",

Could anyone pls tell what I am missing or what is the right way to do it? TIA

3 Answers 3

8

The solution to this issue has been explained in other threads, see: How to compile scss to css with node-sass

However, here's my solution using the run-watch program, nodemon:

npm install node-sass nodemon --save-dev

In package.json, add these to your "scripts":

 "build-css": "node-sass --include-path scss src/input.scss src/styles.css",
 "watch-css": "nodemon -e scss -x \"npm run build-css\" "

Then, in your "start" script, use this:

 "start": "concurrently \"node app.js\" \"npm run watch-css\" "

Next, we need to install the concurrently package:

npm install concurrently --save

To compile and serve your files,

npm start

Therefore, the "npm start" command triggers a build that starts the app and concurrently calls the "run watch-css" command which in turn calls "build-css" to compile scss to css in real-time through nodemon. It compiles and refreshes live when you edit any scss in your text editor.

Sign up to request clarification or add additional context in comments.

Comments

1

Running gulp will run the default task which you haven't included in your gulpfile. You need to add this

gulp.task('default', ['sass','watch']);

2 Comments

ok will try it. but i have gulp watch in gulpfile.js as well as in package.json start script. It should have run, isnt it?
if you run gulp watch by it self, it would only run the watch task without compiling. Therefore you need to add both into a default task.
0

Without gulp

  "start": "tsc && concurrently \"tsc -w\" \"lite-server\"  \"npm run build-css\"",
  "build-css": "node-sass resources/css/sass/style.scss resources/css/style.css",

1 Comment

Thanks @Mircea - will surely try this out!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.