Stop Apple Spotlight from slowing down your Mac by preventing it to index node_modules folders

Speaking of node_modules folders, this tip by Roel Van Gils came to mind:

Hat tip, Roel! But wait dear reader, there’s more …

~

In case you want to retroactively add .metadata_never_index files to your node_modules folders, use this command as suggested by @malfaitrobin (a former student of mine):

$ find . -type d -name "node_modules" -exec touch "{}/.metadata_never_index" \;

You might need to reindex Spotlight after this before it takes effect:

$ sudo mdutil -i on /

~

To stay ahead of things, it’d be much better to create this .metadata_never_index file before running npm install.

As nor npm install (issue) nor yarn install (issue) have the creation of said file integrated, I use a preinstall script that does the job for me.

To enable it, add the line(s) below to your package.json:

"scripts": {
  "preinstall": "mkdir -p ./node_modules && touch ./node_modules/.metadata_never_index"
}

The preinstall script will be run right before npm install does its thing, ensuring the presence of the .metadata_never_index file inside ./node_modules 🙂

Did this help you out? Like what you see?
Consider donating.

I don’t run ads on my blog nor do I do this for profit. A donation however would always put a smile on my face though. Thanks!

☕️ Buy me a Coffee ($3)

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.