Sents File Watcher for Node
In December 2020, I wrote a pure-JS file watcher called
It has zero dependencies and doesn't use any native code (no weird bindings) yet is still quite speedy.
There's also the command-line version, which just has three dependencies in total. You can use it to watch files to rebuild in your projects.
# add to your project npm install --save-dev sents-cli # for info on command-line args sents --help # watch your JS files and rebuild on changes sents "**/*.js" -c "npm run build"
You can specify
-c to run a command, or leave it off just to see a log of changed files, like this:
$ sents "*.json" Watching "*.json" ... add:bleh.json change:bleh.json delete:bleh.json add:BLEH.json
This is a kind of announcement post, even though the code is six months old (I just cleaned up a few issues today). Why should you use this?
Whenever possible, I want my Node dependencies to themselves have zero dependencies—or as few as possible, with an explanation to why they're needed. Every dependency you add (even this library) increases the surface area of your project. (Some projects even mask this by bundling their dependencies further, but that's for a different post.)
No Native Code
Node's native bindings—I'm sure someone understands them, but they present the most amazingly opaque errors for users.
I even wrote a touch about this when I spoke about
I also dispute that tools like file watchers actually need native bindings. Sure, Node's built-in methods aren't great, but all it takes is a tiny bit of massaging of its results to get something performant.
Here are some concessions I've made in
sents to get performance up:
- we don't detect renames (but they don't really make sense in a world of hard links—perhaps you created a hard link and removed the old file, is that a rename?)
- you watch a fixed folder 'path', not its inode—if you watch the folder "/foo/bar" but it moves, sents will either crash or continue to watch any new folder that appears at "/foo/bar"
On Linux, Node can't watch a folder recursively, so we have to recurse and watch every subfolder (unlike on macOS and Windows, where Node's built-in calls support this). But there's not a native method to do this anyway, so every watcher is doing this, and it's not really a concession 🤷
Click the links. Use it in your project!