29

Is there a way to globally ignore folders with a certain name (say node_modules)

I know I can do it manually for each instance but I was wondering if its possible to just let spotlight know not to index these folders? node_modules, for example, typically has thousands of files that I don't want to search and it ends up slowing down spotlight.

I've looked around but I can only find answers that suggest manually adding folders in spotlight privacy list. Does someone have a better, glob way to it?

4
  • 1
    Adding specific folders to the Spotlight privacy is the way to have it ignore during indexing. Why are you against doing it the Apple-provided way?
    – fsb
    Jul 24 '16 at 3:29
  • 8
    Because I want to ignore many instances of this folder and doing that "the apple-provided way" will be time-consuming and ineffective (I might miss some). In addition to that, whenever a new node_modules folder is created, I'll have to go and add it manually.
    – rzr
    Jul 24 '16 at 3:35
  • See also apple.stackexchange.com/questions/87090/…
    – mems
    Jun 6 '17 at 17:20
  • Everyone facing this issue, please submit a request to apple.com/feedback/macos.html so they'll do something about it!
    – Andy
    Nov 17 '20 at 0:30
30

There is no known method to exclude from Spotlight a file or a folder based on a pattern (ex.: its name).

However it's possible to exclude a folder from Spotlight by adding to it an empty file .metadata_never_index.

You can use this method to ignore all node_moduleand bower_modules folders:

find /path/to/projects -type d  -path '*node_modules/*' -prune -o -type d -name 'node_modules' -exec touch '{}/.metadata_never_index' \;

Edit:

It look like the method .metadata_never_index is ignored by Spotlight since Mojave.


Edit 2:

As @JohnLee pointed it out, the extended attribute com.apple.metadata:com_apple_backup_excludeItem is not related / doesn't have any impact on Spotlight.

However *.noindex and symlink aren't indexed by mds (the backend of Spotlight). You can use it as band-aid:

# Rename all node_modules to node_modules.noindex and create a symlink node_modules -> node_modules.noindex
find /path/to/projects -type d \( -path '*node_modules/*' -o -path '*node_module.noindex/*' \) -prune -o -type d -name 'node_modules' -exec mv '{}' '{}.noindex' \; -exec ls -s '{}.noindex' '{}' \;

Note: if you use npm-ci, the node_modules is "automatically removed before npm ci begins its install".

5
  • 6
    I cannot confirm that this works for my Mac with Sierra as of today. I ran the script and can find all the hidden .metadata_never_index files in my node_modules, yet after a forced refresh of the spotlight index, the files are still being indexed. There is also a node_modules NPM Github PR open but I am afraid that this won't be the final solution to the problem github.com/npm/npm/issues/15346 Jun 5 '17 at 16:59
  • Note: Chrome and Opera use it for profile folder. See bugs.chromium.org/p/chromium/issues/detail?id=395300#c5
    – mems
    Jun 6 '17 at 17:14
  • 3
    Sadly .metadata_never_index is not honored on mojave.
    – Karl Pokus
    Sep 4 '19 at 11:51
  • @mems the linked article talks about it being excluded from backups, but doesn't talk about Spotlight. Do you have a source that talks about it in relation to Spotlight?
    – John Lee
    Nov 6 '20 at 23:39
  • @JohnLee you right, it's an attribute used by TimeMachine to exlude files, not Spotlight
    – mems
    Nov 8 '20 at 18:47
12

In case anyone missed fsb's comment in the question, as I did. Apple provides a privacy tab in the Spotlight settings.

Goto "System Preferences" > "Spotlight" > "Privacy" > "+" Add folders to the list.

Note: This may not work immediately, I had to restart Spotlight to see the effects.

0
2

Apparently adding '.noindex' to a parent folder name will prevent Spotlight from indexing.

NB: I haven't personally verified.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .