• Stars
    star
    138
  • Rank 264,508 (Top 6 %)
  • Language
    JavaScript
  • Created about 13 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This module enables code hotswapping in node.js

Basic usage

Just write this in your main file (or in the repl):

require('hotswap');

And then add this line to modules you want to be reloaded (it tells node-hotswap to watch this file):

module.change_code = 1;

Now if you change your modules, they will be reloaded automatically.

Demo (you have to wait more than 1 second between writings - it's file system limitations):

var fs = require('fs');
var hotswap = require('hotswap');

// writing the initial version of test.js
fs.writeFileSync("hotswap-test.js", "module.exports.version = 0; module.change_code = true;");

// requiring it
var test = require('./hotswap-test')
console.log(test);

hotswap.on('swap', function() {
        // we are going to console.log(test) whenever it's changed
        console.log(test);
});

setTimeout(function() {
        fs.writeFile("hotswap-test.js", "module.exports.version = 1; module.change_code = true;")
}, 1000);

setTimeout(function() {
        fs.writeFile("hotswap-test.js", "module.exports.hi_there = function(){}; module.change_code = true;")
}, 2000);

setTimeout(function() {
        fs.writeFile("hotswap-test.js", "module.exports = {wow: 'thats working'}; module.change_code = true;")
}, 3000);

/* outputs:
 * { version: 0 }
 * { version: 1 }
 * { hi_there: [Function] }
 * { wow: 'thats working' }
 */

What does it do?

This module overrides default functions in require.extension and do some magic there. It remembers all references to exports objects of modules with function module.change_code defined. When module is changed, contents of it's old exports object is replaced with contents of the new one.

// So, this will work fine, m will be changed:
var m = require('hot-swapping-module');

// but "hotswap" have to way to replace m.func with new value
// so dont do this unless you really want to use old code, 
var m.func = require('hot-swapping-module').func;

Events

require('hotswap') returns an EventEmitter that emits the following events:

  • change - when one of the watched modules has changed
  • swap - after successful replacing an old module with a new version
  • error - if there was a filesystem error or something like that

Local variables

When old module is replaced by the new one, local variables of the old module will be lost. If you want to save them, you can use module.change_code.

If module.change_code is defined as a function it will be called before module is reloaded. So you can write up something like that:

module.cache = {} // it's important data you want to save

module.change_code = function(oldmod, newmod, exports_object) {
  newmod.cache = oldmod.cache;
}

Configuration

You can configure node-hotswap using configure function.

require('hotswap').configure({
	// a list of extensions registered by hotswap
	//
	// you can define your own extension for such files if you are afraid 
	// that this module would mess up with some other modules (shouldn't 
	// happen though)
    //
	// default: {'.js': 'js', '.coffee': 'coffee'}
	extensions: {'.js': ['js', 'jsx'], '.coffee': 'coffee'},

	// enable or disable fs.watch() on hotswapping files
	// default: true
	watch: true,

	// automatically reload files on change
	// default: true
	autoreload: true,
});

Todo

I should really write up some good documentation and examples here -_-

More Repositories

1

sinopia

Private npm repository server
JavaScript
5,505
star
2

node-fann

FANN (Fast Artificial Neural Network Library) bindings for Node.js
C++
186
star
3

yapm

yapm is a package manager for node.js (npm fork)
JavaScript
77
star
4

jju

Set of utilities to work with JSON / JSON5 documents.
JavaScript
71
star
5

markdown-it-regexp

make simple markdown-it plugins easier
JavaScript
53
star
6

markdown-it.rs

markdown-it js library rewritten in rust
Rust
41
star
7

asset-pipeline

Assets preprocessor/compiler for Express 3.
JavaScript
31
star
8

bevy_mod_physx

PhysX plugin for Bevy
Rust
25
star
9

sinopia-ldap

LDAP auth plugin for sinopia
JavaScript
18
star
10

sinopia-htpasswd

auth plugin for sinopia supporting htpasswd format
JavaScript
11
star
11

render-readme

render and sanitize readme.md just like github would
JavaScript
7
star
12

node-skype

skype dbus interface
CoffeeScript
4
star
13

express-json5

json/json5 body parsing middleware
JavaScript
4
star
14

x-eve

A JavaScript object schema, processor and validation lib (EVE fork)
JavaScript
3
star
15

expose_curl

tool to trace http requests in node.js
JavaScript
3
star
16

yaml-update

Update yaml file preserving structure (comments, linebreaks, etc.), VERY limited
JavaScript
3
star
17

jquery-window

This is a fork of jQuery-Window plugin by firestroke
3
star
18

url-unshort

JavaScript
3
star
19

caching-agent

http.Agent with built-in cache
3
star
20

bevy-rapier-sim-time

Example project of controlling Bevy Rapier simulation speed.
Rust
3
star
21

carsim

just a test, nothing to see here
Rust
2
star
22

iohttp

Drop-in replacement for built-in HTTP parser in io.js
JavaScript
2
star
23

x-yaml

JS-YAML with a friendly interface
JavaScript
2
star
24

express-reqres

req/res prototype extensions extracted from express.js
JavaScript
1
star
25

mdurl.rs

URL parser and formatter that gracefully handles invalid input.
Rust
1
star
26

node-sundown

Node.js port of Sundown (standards compliant, fast, secure markdown processing library in C)
C
1
star
27

bevy_decl_egui

work in progress
Rust
1
star
28

webgl-experiments

1
star
29

pupergrep-2g

a fork of pupergrep for internal use (primary goal is to display json logs here)
JavaScript
1
star
30

yapm-progress

progress bar extracted from yapm
JavaScript
1
star
31

rlidwka.github.com

test
1
star
32

flags

This is very specific piece of software... If you don't know what is it, you almost certainly don't need it at all.
Perl
1
star
33

pagedown

just another fork of pagedown...
JavaScript
1
star
34

error

JavaScript
1
star
35

sinopia-playground

JavaScript
1
star
36

travis-test

JavaScript
1
star