13. Node.js Lessons. Development, Supervisor


Our goal for this lesson is to improve our Node.js development skills. The first thing that will help us in this task – and we’re going to learn it – is called supervisor. We’ve already created an echo server.

However, the development process contained certain difficulties. Any code change required a server reset. It can be easily explained, since Node.js reads files only one time. Having read the file, it receives a respective module object and further uses it. But what should we do for development?
Of course, there is an option to clean up the internal cache of Node.js, but another approach is used more often. We’re talking about a special module that monitors all files from the directory. As soon as something gets changed within the directory and Node.js gets restarted, one of these modules gets named supervisor. This module contains a console utility that has to be accessible using a system pass.

That’s why let us install it globally:
npm i -g supervisor

Now I launch the following instead of node server.js  -> supervisor server.js

What will it bring to us? Now I will change the file. For example, I will add an empty string to it and save the result.

Have you noticed? Crashing child and a new start. Let us do it again. Delete a string and save the result.

WebStorm saves the result just at a single click on the Save button. Or if it loses the focus. It means, whenever I switch to a browser window, it will immediately save the result of my editing.

Supervisor is constantly monitoring the files, and whenever it sees any change of the files with node or js extension by default, it will restart Node.

Moreover, if this directory has other files and they are changed, supervisor will monitor the directory content, too, and see what files have been changed. As soon as any of them changes, including the directory pass, it will start Node. It is a great feature for developing big multi-level projects.

But what pitfalls does supervisor have? The do exist. The first one you need to know concerns new files. Let us create file.js. Save it.

No reaction follows. It happens because supervisor – at least, its current version – monitors each file individually. It simply doesn’t know that a new file has appeared. But if we add the changed file using a require server, it will monitor exactly these server changes and will restart the program. And after this restart it will supervise the new js file, too. So, if adding a new file gets followed by changes in the existing ones – and this is a common scenario – it means everything is ok. But there may be certain challenges, when we will have to restart the supervisor manually. Otherwise, it won’t see new files automatically.

Another pitfall of the supervisor is that if we install a lot of modules and we’ve got a big node.modules directory – and every module generates its own dependencies – the total number of subdirectory files will be enormous, and the supervisor will devour processing time. This kind of thing is quite evident, if you work on a laptop that will get much hotter. To avoid this situation, it is recommended either to launch the supervisor with an ignor parameter and specify what exactly needs to be (node.modules), or move the node.modules somewhere above because these are external modules. They are rarely changed.

So, we will use the supervisor for our further development.

This lesson’s coding can be found in our repository.


The materials were borrowed from the following screencast

We are looking forward to meeting you on our website soshace.com

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles

How I Built an Admin Dashboard with Python Flask

In this article, I will share the how I built an admin dashboard. I will give a step by step guideline on how I built this application with Python ...

Getting started with Git Hooks using ghooks

Git hooks are configured scripts that are executed at specific moments during git operations. ghooks is a package that configures git hooks so that ...

React Native vs. Flutter: Which One Would Suit You Better?

Unveiling pros and cons of such cross-platform development frameworks as Flutter and React Native. Discover what suits you ...

Sign in

Forgot password?

Or use a social network account


By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account


By Signing In \ Signing Up, you agree to our privacy policy