flyinsky76 / node-js-development-mode

auto reload Node.js when modifying application code (hot reload, "development mode")

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Node.js is a good peice of software, but it lacks a very critical feature: a "development" mode.
In "development" mode, if you modify your application code, it get applied "on the fly",
without the need to restart the application server.

Development for Node.js under Windows, thus, has been a pain.
You had to go and change a line of code,
then go to the Node console window,
then close it by clicking the cross icon,
then go to Explorer and run "start node.js.bat",
and then go to your web browser and finally hit "Refresh"
just to find out if that modified line of code works.

This script fixes that drawback, and is used to run Node.js in "development" mode (works since version 0.5.10 of Node.js).
Thanks to the Node community for helping me to make it work.
This script supposedly works on Linux / Mac too, since it uses no platform-specific tricks.

Running:
node "c:\work\node-js-development-mode.js" --main-file "code/web/main.js"
or
node "c:\work\node-js-development-mode.js" --main-file "code/web/main.coffee" --coffee-script "c:/work/node/coffee-script/bin/coffee"

You can also specify files, you want to "--watch" (and to "--ignore"), manually (** and * are supported):

node "c:\work\node-js-development-mode.js" --main-file "code/web/main.js" --watch "['*.js', '*.coffee']"

All the files in the "project directory" are scanned at startup.
By default, this directory is '.', but you can change it via "--project_directory".

** means any number of folders, including no folders at all, so '**/test.js' matches 'test.js', 'test/test.js' and 'test/test/test.js'.
* means any number of characters (in folder or file name), but at least one character must be present. So, '*hat.js' matches 'that.js', but doesn't match 'hat.js' and 'test/that.js'.

You can also pass options to your Node.js application using the --options key.
I pass json options like this: --options "{ \"server\": \"home\" }"
And then in you Node.js application:

function get_launch_options()
{
	var index = process.argv.indexOf('options')
	if (index >= 0)
		return JSON.parse(process.argv[index + 1])
}

console.log(get_launch_options()) // { server: 'home' }

My use case.

I, personally, run Node.js with this script (I just double click it in Explorer), which is placed in the root directory of my project.
"run node.js.bat":

@echo off
title node.js
rem use utf-8 encoding in console output:
chcp 65001
node "c:/work/node-js-development-mode/node-js-development-mode.js" --main-file code/web/main.coffee --coffee-script "node_modules/coffee-script/bin/coffee" --watch "['code/**/*.js', 'code/**/*.coffee']" --options "{ \"server\": \"home\" }"
pause

Remember to pass the "project_directory" argument if you are running this batch file not from the root folder of your project: all matching patterns are applied relative to the root project folder.

Also, make sure you've added path to the "node.exe" file to your system "Path" variable.
(right click "My computer", Properties, blah blah blah, Evironmental variables, find "Path" there, click "Edit", add ";" and path to "node.exe" without trailing slash, "OK")

This script was considerably rewritten by Nikolay Kuchumov (kuchumovn@gmail.com).
The base for this script was initially created by DracoBlue (dracoblue.net) for Linux platform, and is part of the Spludo Framework.
Then I came by it on the internets:
http://dracoblue.net/dev/hot-reload-for-nodejs-servers-on-code-change/173/
and adapted it for my OS.
(It's windoze, cause I don't have money to buy a Mac. If you'd like to assist me in buying a Mac, just email me)
Now, it event works on other platforms.
I'm planning to use it as a supervisor on my server.

You might want to check out
https://github.com/kuchumovn/node-js-development-mode
to issue an error report, or to request a feature, or to just get a new version.

script version: 1.0.0
Licensed under the terms of MIT License.

About

auto reload Node.js when modifying application code (hot reload, "development mode")