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?
Thank me with a coffee.

I don\'t do this for profit but a small one-time donation would surely put a smile on my face. Thanks!

BuymeaCoffee (€3)

To stay in the loop you can follow @bramus or follow @bramusblog on Twitter.

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Unless noted otherwise, the contents of this post are licensed under the Creative Commons Attribution 4.0 License and code samples are licensed under the MIT License

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.