Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp 4 watch() TypeError: expected a string

I have encountered a problem while starting gulp. The info I got is from https://coder-coder.com/gulp-4-walk-through/ and https://www.youtube.com/watch?v=-lG0kDeuSJk.

my gulpfile.js

// Initialize modules
const { src, dest, watch, series, parallel } = require('gulp');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const concat = require('gulp-concat');
const postcss = require('gulp-postcss');
const replace = require('gulp-replace');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const terser = require('gulp-terser');

// File path variables
const files = {
  scssPath: './assets/styles/sass/**/*.scss',
  jsPath: './assets/js/**/*.js'
}

// Sass taks
function scssTask(){
 return src(files.scssPath)
   .pipe(sourcemaps.init())
   .pipe(sass())
   .pipe(postcss([ autoprefixer(), cssnano() ]))
   .pipe(sourcemaps.write('.'))
   .pipe(dest('dist'));
}

 // JS taks
 function jsTask(){
   return src(files.jsPath)
       .pipe(concat('main.js'))
       .pipe(terser())
       .pipe(dest('dist'));
 }

 // Cachebusting taks
 const cbString = new Date().getTime();
 function cacheBustTask(){
   return src(['index.html'])
       .pipe(replace(/cb=\d+/g, 'cb=' + cbString))
       .pipe(dest('.'));
 }     

 // Watch taks
 function watchTask(){
   watch([files.scssPath, files.jsFiles],
     series(
       parallel(scssTask, jsTask),
       cacheBustTask
     )
   );
 }
 // Default task
 exports.default = series(
   parallel(scssTask, jsTask),
   cacheBustTask,
   watchTask
 );

when I start command gulp in the folder i get an error saying:

[09:53:25] 'watchTask' errored after 1.81 ms

[09:53:25] TypeError: expected a string

at module.exports (D:\Web-Development\Startup\practise\sassStructure-Copy\node_modules\is-negated-glob\index.js:5:11)

at sortGlobs (D:\Web-Development\Startup\practise\sassStructure-Copy\node_modules\glob-watcher\index.js:66:18)

at Array.forEach (<anonymous>)

at watch (D:\Web-Development\Startup\practise\sassStructure-Copy\node_modules\glob-watcher\index.js:63:18)

at Gulp.watch (D:\Web-Development\Startup\practise\sassStructure-Copy\node_modules\gulp\index.js:48:10)

at watchTask (D:\Web-Development\Startup\practise\sassStructure-Copy\gulpfile.js:46:3)

at bound (domain.js:420:14)

at runBound (domain.js:433:12)

at asyncRunner (D:\Web-Development\Startup\practise\sassStructure-Copy\node_modules\async-done\index.js:55:18)

at processTicksAndRejections (internal/process/task_queues.js:75:11)

[09:53:25] 'default' errored after 721 ms

How can I resolve this? It expects string and i give it a string jet it doesn't work. I have put

   function watchTask(){
      watch([files.scssPath, files.jsFiles],
        series(
          parallel(scssTask, jsTask),
          cacheBustTask
        )
      );
    }

Then I get no errors, watchTask starts but no files get updated, just silence haha

Here is folder structure: [a link]https://i.ibb.co/LPYsxWG/brisi.png

Could someone explain me why it doesn't know it's string like on everyone's project and i must convert it to string with toString() and why the watch function watchTask doesn't recognize any updates? Thank's in advance.

like image 311
Branko Anokic Avatar asked Dec 03 '25 18:12

Branko Anokic


1 Answers

It seems you are using the wrong variable name. jsPath should be used. Either way, try to make it more readable... for example you can use:

const watch = function() {
    gulp.watch("./assets/styles/sass/**/*.scss", {usePolling : true}, gulp.series(css));
    gulp.watch("./assets/js/**/*.js", {usePolling : true}, gulp.series(js));
};

If you use this code it's not necessary for watch to require gulp in the first line of your code.

In the end make the block like this:

 // Default task
 exports.default = series(
   parallel(scssTask, jsTask),
   cacheBustTask,
   watch
 );

exports.watch = watch;

like image 120
Pikk Avatar answered Dec 06 '25 08:12

Pikk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!