Why isn't grunt-contrib-watch livereload working?

Zach Lysobey picture Zach Lysobey · Feb 20, 2014 · Viewed 14.8k times · Source

I'm struggling to get Grunt's "live reload" ability (as implemented in grunt-contrib-watch) to work in my app. I finally bit the bullet, and tried making a minimal example. Hopefully someone can easily notice what's missing.

File Structure:

├── Gruntfile.js
├── package.json
├── index.html

package.json

{
  "name": "livereloadTest",
  "version": "0.1.0",
  "devDependencies": {
    "grunt": "~0.4.2",
    "grunt-contrib-watch": "~0.5.3"
  }
}

Gruntfile.js

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        watch: {
            src: {
                files: ['*.html'],
                options: { livereload: true }
            }
        }
    });
    grunt.loadNpmTasks('grunt-contrib-watch');
};

index.html

<!doctype html>
<html>
<head><title>Test</title></head>
<body>

<p>....</p>
<script src="//localhost:35729/livereload.js"></script>

</body>
</html>

I then run grunt watch and nothing blows up. However, no browser window opens automatically (should it?).

When I open chrome at http://localhost:35729/ I recieve this json:

{"tinylr":"Welcome","version":"0.0.4"}

and trying any other path on that port gives me

{"error":"not_found","reason":"no such route"}

Answer

Kyle Robinson Young picture Kyle Robinson Young · Feb 20, 2014

http://localhost:35729/ is the URL to the live reload server. It is only used for managing live reload, not serving your actual website.

Typically, one would use grunt-contrib-connect to serve static sites with grunt. Then view their site by going to localhost:8000 or wherever you've configured it to reside. But depending on your needs, it could be apache, nginx, etc serving files as well.

There is a livereload option on grunt-contrib-connect as well. This only injects the <script src="//localhost:35729/livereload.js"></script> tag into your HTML and nothing else.