If you are a node.js developer, you might have faced the situation where you keep on changing the files in your app and continuously stopping and starting the node server. This is really frustrating process. Live reloading can be easily enabled in the node.js applications. In this article I will explain the simple steps to enable live reload feature in node.js app.
Step 1: Install the node-livereload npm package in your app using following command:
$ npm install -g livereload
# OR
$ npm install livereload --save-dev
Step 2: Open the app.js
in your node app and add following lines:
var livereload = require('livereload'); // This is different form http.createServer() or app.createServer() var reloadServer = livereload.createServer(); reloadServer.config.exts.push("jade"); // Enable live reload for jade files reloadServer.watch(__dirname); // Enable watch on complete app folder // You can also enable watch on multiple folders: reloadServer.watch([__dirname + "/js", __dirname + "/css"]);
By default this extension does not support live reloading for .jade
file. If you are using jade
template engine, you need to enable live reloading for .jade
files as shown above.
Step 3: Install the extension
Install the live reload extension for your browser. Do not forget to enable the extension.
Now reload the node server and live reloading is up and running.
Why not we use nodemon or forever?