任务“浏览器同步”不在你的大文件中

我刚开始使用浏览器同步。 所以我开始学习一口饮料。 我使用npm install gulp -g命令npm install gulp -g 。 在此之后,我使用下面的命令npm init初始化了我的项目目录,其中包含package.json文件。 然后,我通过npm install gulp --save-dev命令在我的项目目录中安装了gulp。我还通过npm install browser-sync --save-dev命令npm install browser-sync --save-dev

现在我试图运行浏览gulp browser-sync使用gulp gulp browser-sync命令,这给我一个错误任务“浏览器同步”不在你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']); }); 

我已经试过包括module.exports = gulp; 在开始以及在gulpfile.js的结尾行,但错误仍然存​​在。 我目前的gulp版本是mac OSX 10.12.3上的3.9.1

请帮我卡住了。

问题是你叫你的任务服务器而不是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: "./" } });