So, in my nodeJS server part I need to use function from js file. This file often updated, so i need dynamic load this file every time, when client load page. How I can do this? Thanks!
Loading code from other files is done with require. However, once you've require'd a file, requiring it again will not load the file from disk again, but from a memory-cache which Node maintains. To reload the file again, you'll need to remove the cache-entry for your file before you call require on it again.
Say your file is called /some/path/file.js:
// Delete cache entry to make sure the file is re-read from disk.
delete require.cache['/some/path/file.js'];
// Load function from file.
var func = require('/some/path/file.js').func;
The file containing the function would look something like this:
module.exports = {
func : function() { /* this is your function */ }
};
This very simple script make me happy :) github.com/isaacs/node-supervisor
first:
npm install supervisor -g
second:
supervisor myapp.js
I have some error, but it's works.