RSS

Automatically Restart Node For Application Changes With Grunt

25 Dec

When you are building applications with node at start it might be very annoying to restart node js server when you do a change to a js file. Each time you do a server side non public Js file change you need to restart server to reflect those changes to be seen in client side(browser). This is not necessary to static resources.

One quick way to avoid this workflow is to use grunt to restart your server when there is a file change in your js files. If you are new to grunt, Grunt is node package more of like a build tool that can be used to run various build time tasks in your applications.

First step is installing latest version of grunt in your project. Here make sure you save this as a dev dependency but not as an actual dependency.

npm install grunt  --save-dev

Secondly you have to make sure grunt command line client is installed.

npm install grunt-cli –g

To monitor server side js changes you need to install grunt node monitoring package.

npm install grunt-nodemon  - -save-dev

In your NodeJs project root you need to create gruntfile.js which is basically acts as configuration file for grunt.

node project

You can enter grunt file content as following.

module.exports = function(grunt) {

  grunt.initConfig({
    nodemon: {
      all: {
        script: 'app.js',
        options: {
          watchedExtensions: ['js']
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-nodemon');
  grunt.registerTask('default', 'nodemon');
};

Your application entry point Js file should be included for node mon’s “script” config property value in my case which was “app.js”. Once you have done this all you have to do is just type “grunt” in you terminal.

node autorestart

You can enter “rs” to restart node at any time if you change a server side non public js file you would see nodemon is restarting the node server avoiding you to do it manually and avoiding the annoyance as you would see in terminal window.
node autorestart 2

 
2 Comments

Posted by on December 25, 2015 in ASP.NET, NodeJs

 

Tags: ,

2 responses to “Automatically Restart Node For Application Changes With Grunt

  1. IronHammer

    January 11, 2018 at 3:14 pm

    Excellent. I am new to grunt and trying to write a server-side script. I found some old docs for grunt-execute I could never get working but grunt-nodemon gave me no trouble at all using your tut. Thanks!

    Liked by 1 person

     
    • Dimuthu

      February 8, 2018 at 8:45 am

      pleasure it helped you

      Like

       

Leave a reply to IronHammer Cancel reply