auto-reload

0.1.0 • Public • Published

node-reload

For node.js to reload the module ,which was to be load by require, automaticly.

Install

npm install auto-reload

Example

Reload Json

test/data/json.json

{ "name" : "Alan" }

test/json.js

const fs = require('fs');
const reload = require('auto-reload');
const data = reload('./data/json'); // reload after file changed
 
// print data every sec
setInterval(() => {
  console.log(data.rand, data.list[0].name);
}, 1000);
 
// update data.json after startup
setTimeout(() => {
  const text = `{
  "rand": ${Math.random()},
  "list": [{
    "name": "Test"
  }]
}`;
  fs.writeFileSync(path.join(__dirname, './data/json.json'), text);
}, 0);

Result:

0 'Alan'
0 'Alan'
0 'Alan'
0 'Alan'
0.41179045320583496 'Test'

Reload Js file

Realod function haven't support

test/data/code.js

module.exports = {
  num: 0,
  str: 'string',
  obj: {
    name: 'Alan',
    age: 18
  },
  list: [1,3,5,7,9]
};

test/code.js

const reload = require('../');
const data = reload('./data/code');
 
// print data from module every sec
setInterval(function() {
  console.log(new Date, data);
}, 1000);
 
// If you update the `time` in test/data/code.js
// the output will change immediately

Aims

  • auto reload json file
  • auto reload js file
  • fix memory leak
  • provide more options (include logger etc.)
  • rewrite with promise (include improve exception catch)
  • add tests with ava

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.1.0
    15
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.1.0
    15
  • 0.0.5
    0
  • 0.0.2
    0
  • 0.0.1
    0

Package Sidebar

Install

npm i auto-reload

Weekly Downloads

14

Version

0.1.0

License

MIT

Unpacked Size

9.69 kB

Total Files

17

Last publish

Collaborators

  • lellansin