Maros Hluska

Avoiding node_modules in Dropbox


Since Dropbox doesn't support ignoring directories programmatically, we can use rsync and crontab to achieve something similar:

If we have many projects with deep node_modules directories, we will want to avoid syncing them directly to Dropbox as it may needlessly block important files from syncing. Instead, we can work from ~/projects/ but periodically sync back to ~/Dropbox/.projects/ with auto-generated directories exluded. A dotfile is used to avoid accidentally working within Dropbox.

Lets rename our existing projects and strip them of noise:

$ mv ~/Dropbox/projects ~/Dropbox/.projects
$ find ~/Dropbox/.projects \( \
  -name 'node_modules' -o \
  -name 'bower_components' -o \
  -name 'tmp' -o \
  -name 'temp' -o \
  -name 'Icon?' -o \
  -name '.DS_Store' \) -print0 | xargs -0 rm -rf

Now lets add a couple of scripts to the projects directory: and

Contents of


DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

rsync -avh --delete ~/Dropbox/.projects/ ~/projects
(crontab -l ; echo "*/10 * * * * ${DIR}/") | sort - | uniq - | crontab -

We can run once as part of a fresh system installation. It will copy over all projects to the home directory and add a crontab entry to sync back to Dropbox every 10 minutes.

Contents of


rsync -avh --delete \
  --exclude=node_modules \
  --exclude=bower_components \
  --exclude=tmp \
  --exclude=temp \
  --exclude=Icon? \
  --exclude=.DS_Store ~/projects/ ~/Dropbox/.projects

The script gets called by crontab and simply syncs back to Dropbox with various auto-generated entries excluded.

With a project named foo, your files should now look something like this after running

$ find ~/projects

$ find ~/Dropbox

$ crontab -l
*/10 * * * * /Users/mhluska/Dropbox/.projects/dotfiles/sync