Furqan Shaikh
Jul 22, 2017 · 2 min read

Watching for Node file changes


When developing Node-based applications, we start the node process by running either node <filename> or executing a specific start script. During development, we will be making frequent changes to the source code files. These changes will not be automatically reflected in the running Node application. The source code files are stored in memory (Resident Set -> Code Segment), and we need to restart the Node process every time the changes are made. Frequent killing of process can be automated by using various tools.

Lets take a look at using one of these tools “nodemon”.


Create a sample Nodejs application (server.js) as shown below:

var http = require(‘http’);

var port = process.env.port || 1337;

http.createServer(function (req, res) {

res.writeHead(200, { ‘Content-Type’: ‘text/plain’ });

res.end(‘Hello World\n’);

}).listen(port);

Install nodemon globally:

npm install -g nodemon

Run the node application:

nodemon server.js

This should launch the node process listening at port 1337

Nodemon monitoring for file changes

Make changes to server.js. For eg: change the response to : “Hello World Modified”. You should see the node process restarted by nodemon and GET request now returning the new response string.

Nodemon restarting node process

Tools

Apart from nodemon , there are other tools as well which serve similar purpose.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade