How to setup hot reload in Node.js

Node

How to setup hot reload in Node.js

We all love Node for its simplicity. We can create a basic index.js file and start writing an application. We just type node index.js inside our terminal and our script will execute when we want to run it.

However, we will soon notice that any additional changes made to our code, in case that we are running a http server or some other long running process, will not restart our script and our code will not have an immediate effect.

We would have to manually terminate our script and run node index.js again.

During development, this would probably drive us insane. Not even mentioning how unproductive and inefficient this would be.

In order to mitigate this problem, we can utilize one small, but highly effective tool called nodemon. When we run our script with nodemon, it will actively lookout for any code changes and restart our script if any were detected.

We can install nodemon from NPM.

npm i –g nodemon


Now instead of

node index.js


we can do

nodemon index.js


This setup will work perfectly fine but we can one up this even further.

We can add an additional script in package.json called start or any other name that you prefer

{
  ...
  "scripts": {
    "start": "nodemon index.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  ...
}


Now instead of

nodemon index.js


 we can just run

npm start


Sweet, isn’t it.

Show comments

Laravel

5 min

How to make Laravel authentication

Laravel provides a neat function that quickly generates a scaffold of routes, views, and controllers used for authentication.

Laravel

7 min

How to install Laravel application

This article will cover how to install and create a local development environment for your Laravel application. There are only a couple of steps that needs to be done.

Laravel

3 min

How to set appropriate Laravel permissions on Linux server

After publishing your Laravel application to a real web server, you discover then some of your functionalities are failing.

Codinary