watch-require
v1.2.0
Published
Require a script and watch for changes
Downloads
95
Maintainers
Readme
This is a small library which requires any NodeJS script and watches for changes. If changes are detected, an event is emitted and the exports object is updated.
const watchRequire = require('watch-require')
const util = require('util')
const watched = watchRequire(path.resolve('index.js'))
function dumpExports(exports) {
console.log(util.inspect(exports))
}
watched.on('change', dumpExports)
dumpExports(watched.exports)
API
watchRequire(modulePath)
Requires the module which is present on the module path.
watched.exports
Contains the module.exports
of the script that is being watched.
watched.on('change', cb)
Will run cb
each time the script or one of its required files changed on
disk. The callback is called with a single argument which holds the new exports
of the script, as if a fresh require()
had been done.
watched.forceReload()
Forces a manual reload of the required script, updating the exports
object.
This method will flush the cache for the current watcher. That is, other wachters will remain attached to old versions of the script, while this script and all of its dependencies will get updated.
License
The MIT License
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.