Skip to content
Create a gist now

Instantly share code, notes, and snippets.

a console app for Windows that monitors changes to a directory and restarts node.js for me
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace ezekiel
class Program
static DateTime lastRead = DateTime.MinValue;
static string monitoring = @"C:\node.js\stuff";
static string startup = @"server.js";
static int processId;
static void Main(string[] args)
var watcher = new FileSystemWatcher();
watcher.Path = monitoring;
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
watcher.Filter = "*.js";
watcher.Changed += Changed;
watcher.Created += Changed;
watcher.Deleted += Changed;
watcher.Renamed += Renamed;
// Begin watching
watcher.EnableRaisingEvents = true;
Console.WriteLine("Press Enter to exit");
static void Kill() {
var matches = Process.GetProcessesByName("node");
matches.ToList().ForEach(match => {
Console.WriteLine("attempting to close node.js [" + match.Id + "]");
Console.WriteLine("successfully closed");
static void Start() {
Console.WriteLine("starting node.js");
var start = new ProcessStartInfo();
start.FileName = @"C:\node.js\node.exe";
start.UseShellExecute = false;
start.CreateNoWindow = true;
start.RedirectStandardOutput = true;
start.RedirectStandardInput = true;
start.Arguments = Path.Combine(monitoring, startup);
var node = new Process();
node.EnableRaisingEvents = true;
node.OutputDataReceived += OutputHandler;
node.StartInfo = start;
Console.WriteLine("[" + node.Id + "] node.exe started");
processId = node.Id;
// close the input, we won't use it
var input = node.StandardInput;
static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
if (!String.IsNullOrEmpty(outLine.Data))
static void Renamed(object sender, System.IO.RenamedEventArgs e)
static void Changed(object sender, System.IO.FileSystemEventArgs e)
// sometimes a text editor makes multiple operations in a single save
// we only handle the first
var lastWriteTime = File.GetLastWriteTime(e.FullPath);
if (lastWriteTime != lastRead)
lastRead = lastWriteTime;

Hi, I've been using this for a while now (I'm still new with node).
I've found it to work better than some native hot reloaders that I've tried.
They tend to run my atom at 90% usage... :)

Hope you don't mind, but I've packaged this up in a solution with a few fixes.
If you have any objections whatsoever, please notify me if you want me to take it down.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.