Skip to content

Instantly share code, notes, and snippets.

@jimmywarting
Last active February 20, 2024 21:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimmywarting/76e5752655deba2d25b63313f47e9e78 to your computer and use it in GitHub Desktop.
Save jimmywarting/76e5752655deba2d25b63313f47e9e78 to your computer and use it in GitHub Desktop.
Simple dependency free file auto-reload (NodeJS)
import { watch } from 'node:fs/promises'
import { Worker } from 'node:worker_threads'
let worker = new Worker('./app.js')
async function reloadOnChange (dir) {
const watcher = watch(dir, { recursive: true })
for await (const change of watcher) {
if (change.filename.endsWith('.js')) {
worker.terminate()
worker = new Worker('./app.js')
}
}
}
// All the folder to watch for
['./src', './lib', './test'].map(reloadOnChange)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment