简体   繁体   English

gulp nodemon不起作用

[英]gulp nodemon doesn't work

Here is my gulpfile.js: 这是我的gulpfile.js:

var gulp = require('gulp'),
  nodemon = require('gulp-nodemon'),
  plumber = require('gulp-plumber'),
  livereload = require('gulp-livereload');

gulp.task('develop', function () {
  livereload.listen();
  nodemon({
    script: 'app.js',
    ext: 'js ejs html coffee'
  }).on('restart',function() {
    console.log('Livereload reload...');
    livereload.reload();
  });  
});

gulp.task('default', [
  'develop'
]);

My app.js: 我的app.js:

var express = require('express');

var app = express();

app.set('view engine','ejs');

app.get('/', function(req,res){
   res.send('Hello world!');
});

app.listen(8888, function() {
  console.log('Server started at 8888');
});

When I change 'Hello world!' 当我更改“ Hello world!” to 'Hello world!!!!!', I can see the following in my console: 到“ Hello world !!!!!”,我可以在控制台中看到以下内容:

在此处输入图片说明

It did logged my changes. 它确实记录了我的更改。 But the page is not reloaded at all. 但是页面根本没有重新加载。 I have to refresh my browser to see the change. 我必须刷新浏览器才能看到更改。 Anything wrong in my gulpfile.js? 我的gulpfile.js有什么问题吗? Any idea? 任何想法? Thanks. 谢谢。

I see that the question is pretty old but, anyway, livereload will only work if your browser supports it. 我看到这个问题已经很老了,但是无论如何,livereload仅在您的浏览器支持的情况下才起作用。 For example, for Chrome you need to install an extension called LiveReload . 例如,对于Chrome,您需要安装一个名为LiveReload的扩展程序 This will enable refreshing the page for you (don't forget to enable it). 这将为您刷新页面(不要忘记启用它)。

Another option is to use BrowserSync . 另一种选择是使用BrowserSync It doesn't actually need anything else besides the plugin (no browser extensions) which you can use in your gulp task. 除了可以在gulp任务中使用的插件(没有浏览器扩展)外,它实际上不需要其他任何东西。 This is an example (taken from here ): 这是一个示例(从此处获取 ):

var gulp = require('gulp');
var browserSync = require('browser-sync');
var nodemon = require('gulp-nodemon');
var BROWSER_SYNC_RELOAD_DELAY = 500;

gulp.task('nodemon', function (cb) {
var called = false;
  return nodemon({
    // nodemon our expressjs server
    script: 'app.js',

    // watch core server file(s) that require server restart on change
    watch: ['app.js']
  })
    .on('start', function onStart() {
      // ensure start only got called once
      if (!called) { cb(); }
      called = true;
    })
    .on('restart', function onRestart() {
      // reload connected browsers after a slight delay
      setTimeout(function reload() {
        browserSync.reload({
          stream: false
        });
      }, BROWSER_SYNC_RELOAD_DELAY);
    });
});

gulp.task('browser-sync', ['nodemon'], function () {

  // for more browser-sync config options: http://www.browsersync.io/docs/options/
  browserSync({

    // informs browser-sync to proxy our expressjs app which would run at the following location
    proxy: 'http://localhost:3000',

    // informs browser-sync to use the following port for the proxied app
    // notice that the default port is 3000, which would clash with our expressjs
    port: 4000,

    // open the proxied app in chrome
    browser: ['google-chrome']
  });
});

Everything is alright with your nodemon installation. 您的nodemon安装一切正常。 You actually have to refresh your browser to see the changes you made. 实际上,您必须刷新浏览器才能看到所做的更改。

If you want to have that done for you, check out guard-livereload at this website . 如果您想为您完成此任务,请在此网站上查看guard-livereload

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM