I
I
inzoddex2019-06-26 12:59:55
JavaScript
inzoddex, 2019-06-26 12:59:55

Error when running gulp.watch "SyntaxError: Identifier 'watch' has already been declared" how to fix?

Error when executing gulp command

Show error
function watch() {
^
SyntaxError: Identifier 'watch' has already been declared
at new Script (vm.js:80:7)
at createScript (vm.js:274:10)
at Object.runInThisContext (vm.js:326:10)
at Module._compile (internal/modules/cjs/loader.js:664:28)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
at Module.load (internal/modules/cjs/loader.js:600:32)
at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
at Function.Module._load (internal/modules/cjs/loader.js:531:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:22:18)

I can't figure out what's wrong, please help me. Code below
const { gulp, series, parallel, src, dest, watch } = require("gulp");
const browserSync = require('browser-sync').create();
const phpMinify = require('@cedx/gulp-php-minify');
const autoprefixer = require('gulp-autoprefixer');
const uglify = require('gulp-uglify');
const less = require('gulp-less');
const minifyCSS = require('gulp-csso');
const concat = require('gulp-concat');
const del = require('del');
const dir = {
    "src": {
        php: './app/*.php',
        html: './',
        css: './app/src/less/**/*.less',
        js: './app/src/js/**/*.js',
    },

    "dest": {
        dest: './build/*',
        php: './build/',
        html: './',
        css: './build/css/',
        js: './build/js/',
    }
}

function php() {
    return src(dir.src.php, { read: false })
        .pipe(phpMinify({ mode: 'fast' }))
        .pipe(dest(dir.dest.php))
}

function css() {
    return src(dir.src.css)
        .pipe(less())
        .pipe(autoprefixer())
        .pipe(minifyCSS())
        .pipe(dest(dir.dest.css))
        .pipe(browserSync.stream())
}

function js() {
    return src(dir.src.js, { sourcemaps: true })
        .pipe(concat('app.min.js'))
        .pipe(uglify())
        .pipe(dest(dir.dest.js, { sourcemaps: true }))
        .pipe(browserSync.stream())
}

function clean() {
    return del(dir.dest.build);
}

function watch() {
    browserSync.init({
        server: {
            baseDir: './app'
        }
    });
    gulp.watch(dir.src.css, css)
    gulp.watch(dir.src.js, js)
    gulp.watch(dir.src.php).on('change', browserSync.reload);
}

exports.js = js;
exports.css = css;
exports.php = php;
exports.clean = clean;
exports.watch = watch;
exports.build = series(clean, parallel(css, js))
exports.default = series(build, watch);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Suha, 2019-06-26
@andreysuha

I'm not great with gulp, but try renaming your watch function

I
Ivan, 2019-06-26
@BenderIsGreat34

I think the problem is that there is a watch function, but there is no task.
my code

gulp.task('watch', function() {
  gulp.watch('src/sass/*.scss', gulp.series('sass'));
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question