2017-02-18 8 views
1

私は、browsersyncを使い始めました。だから、私はギャルプについて学び始めました。 npm install gulp -gコマンドを使用してgulpをインストールしました。この後、コマンドnpm initに従って、プロジェクトディレクトリをpackage.jsonファイルで初期化しました。私はnpm install gulp --save-devコマンドで自分のプロジェクトディレクトリにgulpをインストールしました。また、npm install browser-sync --save-devコマンドでbrowsersyncをインストールしました。 タスク 'browser-sync'はあなたのgulpファイルにありません

は今、私は私にエラー タスク「browsersync」を与えgulp browser-syncコマンドでがぶ飲みを使用してbrowsersync実行しようとした私のgulpfiles.jsが提出され、あなたのgulpfile以下

をしていない

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です

私は立ち往生してください。

答えて

4

問題は、あなたがbrowserSyncをあなたのタスクserveと呼ばれていない:私は疑問を持っている

// 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: "./" 
    } 
}); 
+0

:あなたはちょうどその名前を変更することができ

// 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: "./" } }); 

。名前の異なる複数のタスクがありますが、なぜこのタスクを選択したのでしょうか。ブラウザ同期のドキュメントからの例です。 – geeksal

+0

私はあなたのブラウザ同期サーバを初期化するものなので、そのタスクを選択しました。 現在のコードで 'gulp serve'を実行しようとしましたが、browserSyncが正常に動作していますか? –

+0

ありがとう!それは今動作します – geeksal

関連する問題