任务 'browser-sync' 不在您的 gulpfile 中

Task 'browser-sync' is not in your gulpfile

我刚开始使用browsersync。所以我开始学习 gulp。 我使用 npm install gulp -g 命令安装了 gulp。在此之后,我通过以下命令 npm initpackage.json 文件初始化了我的项目目录。然后我通过 npm install gulp --save-dev 在我的项目目录中安装了 gulp command.I 还通过 npm install browser-sync --save-dev 命令安装了 browsersync。

现在我尝试通过 gulp browser-sync 命令使用 gulp 运行 browsersync,这给了我一个错误 Task 'browser-sync' is not in your gulp文件

下面是我的gulpfiles.js文件

var gulp        = require('gulp');
var browserSync = require('browser-sync').create();

// process JS files and return the stream.
gulp.task('js', function () {
  return gulp.src('js/*js')
    .pipe(browserify())
    .pipe(uglify())
    .pipe(gulp.dest('dist/js'));
});

// create a task that ensures the `js` task is complete before
// reloading browsers
gulp.task('js-watch', ['js'], function (done) {
    browserSync.reload();
    done();
});

// use default task to launch Browsersync and watch JS files
gulp.task('serve', ['js'], function () {

// Serve files from the root of this project
browserSync.init({
    server: {
        baseDir: "./"
    }
});

// add browserSync.reload to the tasks array to make
// all browsers reload after tasks are complete.
  gulp.watch("js/*.js", ['js-watch']);
});

我已经尝试在 gulpfile.js 的开头和结尾包含 module.exports = gulp; 行,但错误仍然存​​在。我当前的 gulp 版本是 mac OSX 10.12.3

上的 3.9.1

请帮助我卡住了。

问题是你调用了你的任务 serve 而不是 browserSync:

// use default task to launch Browsersync and watch JS files
// NOTE how this task is called serve:
gulp.task('serve', ['js'], function () {

// Serve files from the root of this project
browserSync.init({
    server: {
        baseDir: "./"
    }
});

您可以更改其名称:

// use default task to launch Browsersync and watch JS files
// NOTE at the name of the task now
gulp.task('browser-sync', ['js'], function () {

// Serve files from the root of this project
browserSync.init({
    server: {
        baseDir: "./"
    }
});