question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Watch errors on gulp-sass when saving file

See original GitHub issue

While having a gulp watch task running, sometimes a file save triggers a SASS-ing, which will provide the following error. Resaving the file will usually solve the problem. sometime more than 1 resave is needed.

ERROR:

[watcher] File d:\....y\scss\breakpoints\_768up.scss was changed, compiling...
[14:02:08] Starting 'sass'...
[14:02:08] Finished 'sass' after 5.16 ms
[gulp-sass] library\scss\style.scss
Error: File to import not found or unreadable: breakpoints/768up
       Parent style sheet: stdin
        on line 64 of stdin
>>      @import "breakpoints/768up";
   -^

I upgraded gulp-sass just last night. I had this error also before the update, but it was a bit less detailed: only the Error: File to import not found or unreadable: breakpoints/768up

Using Windows 8.1 Gulpfile:

'use strict';

// Include gulp
var gulp = require('gulp'); 

// Include Our Plugins
var jshint = require('gulp-jshint');
 var sass = require('gulp-sass');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
// var livereload = require('gulp-livereload');
var prefix = require('gulp-autoprefixer');

var paths = {
    styles: {
        src: './library/sass',
        files: './library/scss/**/*.scss',
        dest: './library/css'
    }
}

var displayError = function(error) {

    // Initial building up of the error
    var errorString = '[' + error.plugin + ']';
    errorString += ' ' + error.message.replace("\n",'\n'); // Removes new line at the end - Q nope

    // If the error contains the filename or line number add it to the string
    if(error.fileName)
        errorString += ' in ' + error.fileName;

    if(error.lineNumber)
        errorString += ' on line ' + error.lineNumber;

    // This will output an error like the following:
    // [gulp-sass] error message in file_name on line 1
    console.error(errorString);
}


// Lint Task
gulp.task('lint', function() {
    return gulp.src('library/js/*.js')
        .pipe(jshint())
        .pipe(jshint.reporter('default'));
});

// Compile Our Sass
gulp.task('sass', function() {
    gulp.src(paths.styles.files)
    .pipe(sass({
        // outputStyle: 'compressed',
        sourceComments: 'map'
        // includePaths : [paths.styles.src]
    }))
    // If there is an error, don't stop compiling but use the custom displayError function
    .on('error', function(err){
        displayError(err);
    })
    // Pass the compiled sass through the prefixer with defined 
    .pipe(prefix(
        'last 2 version', 'safari 5', 'ie 8', 'ie 9', 'opera 12.1', 'ios 6', 'android 4'
    ))
    // Funally put the compiled sass into a css file
    .pipe(gulp.dest(paths.styles.dest));
    // .pipe(livereload());
});

// Concatenate & Minify JS
gulp.task('scripts', function() {
    return gulp.src('library/js/*.js')
        .pipe(concat('all.js'))
        .pipe(gulp.dest('dist'))
        .pipe(rename('all.min.js'))
        .pipe(uglify())
        .pipe(gulp.dest('dist'));
});

// Watch Files For Changes
gulp.task('watch', function() {
    // livereload.listen();
    gulp.watch('library/js/*.js', ['lint', 'scripts']);
    gulp.watch('library/scss/**/*.scss', ['sass'])
    .on('change', function(evt) {
        console.log(
           '[watcher] File ' + evt.path.replace(/.*(?=sass)/,'') + ' was ' + evt.type + ', compiling...'
        );
    });
});

// Default Task
gulp.task('default', ['lint', 'sass', 'scripts', 'watch']);

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:12

github_iconTop GitHub Comments

3reactions
chekitcommented, Jun 21, 2016

I’m faced with the same issue. But in my file structure I’m using node modules, so I’ve just set setTimeout to my require of scss task and it seems to work for me.

For example:

> scss.gulp.js:

'use strict';

const gulp = require('gulp');
const sass = require('gulp-sass');
const notify = require('gulp-notify');

let sassTask = function () {
    return gulp.src('./src/scss/styles.scss')
        .pipe(
            sass()
            .on('error', notify.onError({
         message: "Error: <%= error.message %>",
             title: "Error in Sass"
        }))
        )
        .pipe(gulp.dest('./dist/css/'));
}

module.exports = sassTask;

> watch.gulp.js:

'use strict';

const gulp = require('gulp');
const watch = require('gulp-watch');

const paths = {sass:    './src/scss/**/*.scss'};
gulp.task('watch', function () {
  watch(paths.sass, () => setTimeout(require('./scss.gulp'), 400));
});
1reaction
brojectcommented, Mar 10, 2017

timeout is very best! now @import Error: File to import not found or unreadable: IS SOLVED my simple is 👍

var admin_css_task = function () { config.sass.includePaths = _adminSassInc; gulp.src(_adminSassMain) .pipe(sass(config.sass)) .pipe(gulp.dest(_adminCssDest)); }; gulp.task(‘admin-css’, admin_css_task);

gulp.task(‘watch’, function () { gulp.watch(_adminSass, function () { setTimeout(function () { admin_css_task(); }, 500); }); }); gulp.task(‘run’, [‘admin-css’, ‘watch’]);

Read more comments on GitHub >

github_iconTop Results From Across the Web

Gulp watch is not working while saving sass file - Stack Overflow
I am new to gulp and I want to activate gulp watcher feature so as to make changes in css file when saving...
Read more >
Using Gulp with Dart Sass - Zell Liew
Watching Sass files for changes. When a Sass file changes, we want to compile the new Sass into CSS. We can do this...
Read more >
gulp-sass - npm
Gulp plugin for sass. Latest version: 5.1.0, last published: a year ago. Start using gulp-sass in your project by running `npm i gulp-sass`....
Read more >
How to fix error "cannot find module gulp-sass" in Gulp
Three quick fixes for the error message saying Gulp can't find the module ... gulp-sass --save-dev to install and save in package.json file....
Read more >
Gulp file runs first time, but errors on watch task 'elixir' when saving a ...
The problem: My gulp file goes through the full list of tasks on launching it, but if I change and save my main.css...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found