How to ignore node_modules recursively in Dropbox on a Mac

Viewed 551

I want to keep all my node projects saved in Dropbox except I want to exclude all node_modules directories from being synced to Dropbox.

I used to accomplish this by keeping these directories symlinked in a different location but recent versions of npm have broken the ability to do this by not allowing npm_modules to be a symlink.

1 Answers

The following command will find and add all top-level node_module directories from Dropbox. That is to say that it will add node_module directories but not node_module directories somewhere inside of another node_modules directory (because the top-level directory will already be ignored.

find ~/Dropbox  -type d | grep 'node_modules$' | grep -v '/node_modules/' | xargs -I {} -t -L 1 xattr -w com.dropbox.ignored 1 "{}"

Command breakdown:

  • find all directories in ~/Dropbox
  • use grep to filter to only the directories that end in node_modules
  • use grep to exclude from these directories that also include node_modules/ **
  • use xargs to pass this directory as an argument
  • to the xattr command to tell dropbox to ignore it

** If a directory ends with node_modules but also includes node_modules/ then that directory is contained in a higher level node_modules directory so we can skip it because we will already be ignoring the higher level directory.

Related