标识符 'browserSync' 已声明

Identifier 'browserSync' has already been declared

我一直收到错误 "Identifier 'browserSync' has already been declared" 但我看不出问题出在哪里 is.Here 我的代码

// Watch files
function watchFiles() {
    gulp.watch("*.js", gulp.series(scriptsLint, scripts, browserSyncReload));
    gulp.watch(["processHTML"], gulp.series(browserSyncReload));
  }

//Task Live Reload

function browserSync(done) {
    browserSync.init({
      server: './dist',
      port: 8080,
      ui: {
        port: 8081
      }
    })
    done()
  };

// BrowserSync Reload
function browserSyncReload(done) {
    browserSync.reload();
    done();
  }

// define complex tasks
const js = gulp.series(scriptsLint, scripts);
const build = gulp.parallel(processHTML,js);
const watch = gulp.parallel(watchFiles, browserSync);

您需要将函数 browserSync 重命名为其他名称,因为这是为 BrowserSync 库保留的关键字。

像这样:

// Watch files
function watchFiles() {
    gulp.watch("*.js", gulp.series(scriptsLint, scripts, reload));
    gulp.watch(["processHTML"], gulp.series(reload));
  }

//Task Live Reload

function localServer(done) {
    browserSync.init({
      server: './dist',
      port: 8080,
      ui: {
        port: 8081
      }
    })
    done()
  };

// BrowserSync Reload
function reload(done) {
    browserSync.reload();
    done();
  }

// define complex tasks
const js = gulp.series(scriptsLint, scripts);
const build = gulp.parallel(processHTML,js);
const watch = gulp.parallel(watchFiles, localServer);

您在第 9 行中声明的 browserSync() 函数的名称与其作用域中的另一个变量相同,browserSync(在第 10 行中),需要重命名。

// Watch files
function watchFiles() {
    gulp.watch("*.js", gulp.series(scriptsLint, scripts, browserSyncReload));
    gulp.watch(["processHTML"], gulp.series(browserSyncReload));
  }

//Task Live Reload

function browserSyncFunc(done) {
    browserSync.init({
      server: './dist',
      port: 8080,
      ui: {
        port: 8081
      }
    })
    done()
  };

// BrowserSync Reload
function browserSyncReload(done) {
    browserSync.reload();
    done();
  }

// define complex tasks
const js = gulp.series(scriptsLint, scripts);
const build = gulp.parallel(processHTML,js);
const watch = gulp.parallel(watchFiles, browserSyncFunc /* I'm guessing you meant to use the browserSync function here, not the object */);