Synching into cloud directories (Dropbox etc.) to ignore node_modules

[2019-12-17] dev, nodejs
(Ad, please don’t block)

In this blog post, I describe how you can ignore node_modules by bidirectionally syncing into a cloud directory (as managed via Dropbox etc.).

node_modules make sync services difficult to use  

Challenge:

  • Especially with multiple devices, cloud syncing is really convenient and complements version control nicely.
  • All sync services that I have tried, break down when faced with node_modules, due to the sheer amount of small files.

Possible solutions:

  1. Several Node.js package managers are exploring mostly eliminating node_modules. This will be a good mid-term solution.
  2. Configuring sync services so that they ignore directories named node_modules. Alas, the basic versions of Dropbox, iCloud, and Google Drive don’t let you do that at the moment.
  3. Bidirectionally synchronizing into a cloud directory and ignoring node_modules while doing so.

We’ll look into (3) next.

Unison  

Unison is a tool for bidirectional file synchronization that runs on macOS, Unix, and Windows. Its website describes how to install it. Caveat on macOS: If you want the watch mode to work, a tool called unison-fsmonitor must be available via the shell. I’m using autozimu’s unison-fsmonitor and it works well for me.

The following shell script watches and syncs continuously:

unison -repeat watch -copyonconflict -prefer newer \
  -ignore 'Name {node_modules,.DS_Store}' \
  '/Users/rauschma/Projects/' \
  '/Users/rauschma/Library/Mobile Documents/com~apple~CloudDocs/Projects'

Options:

  • -repeat watch: Unison watches both sync directories for changes and syncs incrementally when it detects any.
  • -copyonconflict -prefer newer: Resolve conflicts automatically.
  • -ignore: Ignore the specified files during syncing.

Bonus  

  • For now, I’m starting the script manually. Eventually, I want to do this automatically on macOS, via launchd.