-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exclude node_modules folders from Spotlight indexing #6453
Comments
It seems like this only just became an issue when updating macOS to Mojave 🤷♂️ |
@burkeshartsis I thought node_modules polluting spotlight search results and high CPU usage from indexing always been an issue. |
See also npm/npm#15346 |
15346 says the "true" solution is add folder names to the index. This is pure nonsense. It should be possible to add the metadata_never_index, OR to set a file attribute. |
*** UPDATE *** .metadata_never_index doesn't work on Majave or Catalina Found this quick script to add .metadat_never_index to your node_modules folders.
I added it as a bash alias, so I can use apply quickly.
Just thought I'd share here, for anyone else that is looking. :) |
I am sorry but will yarn consider this feature? |
Based on @chrisbull 's idea, this script does the same thing faster by not going inside any node_modules directories found (coz the whole node_modules directory had already been ignored)
For myself, I run it on the home directory, so I exclude hidden directories and Library directory to speed things up
I also use the -print option so I know which folder is affected |
For those using @dicksonhk's answer (thanks!) on MacOS, you have to have a dot or other directory after the command "find". Maybe the same for other linux. I agree that yarn should automatically add this file, although this is MacOS specific. Maybe some hook is better? |
@justin808 You're right! I edited my answer to add |
My Spotlight seems to ignore these |
Until this gets added — if it will — another approach would be to add a "postinstall": "touch ./node_modules/.metadata_never_index" Also, I didn't manage to make @dicksonhk's script to work (maybe because I'm using fish, dunno), but I did find another handy little script: https://github.com/Willian-Zhang/Ignore-node_modules-in-Spotlight. |
@rolandleth I think it makes more sense to use "preinstall", so spotlight doesn't start picking up the files while the install process is ongoing. But now I tried it is seems that postinstall and preinstall both don't add the file to my node_modules. Even if I delete the folder and run install again 🤔 |
"preinstall": "mkdir -p node_modules; touch ./node_modules/.metadata_never_index" |
Ha, nice call. Sadly, the file gets deleted after |
Guys does adding .metadata_never_index really work? Because it's still indexing node_modules and subfolders. I've ran
And noticed that it still indexing node folders/subfolders |
Same. As I had said before, |
I've read that |
No, doesn't work for me either. Spotlight still indexes everything... |
I did some research on that This works in a different way. If you want the folder |
Potential workaround is to put all projects in a single folder, black-list that folder via spotlight privacy settings and then create a VScode workspace outside that folder which points to the project folder internally. |
I've glued together solutions found here and around the internets and automated it:
It's definitely not a code that I would be proud of, but it works :) https://github.com/Strajk/setup/blob/master/programs/prevent-spotlight-from-indexing-node-modules.js |
Is this actually working for anyone?? Find candidates are not evaluated?? |
@wiegell still working for me :) can you clarify what do you mean by not being evaluated? I've written the script intentionally in a very simple, step-by-step style so it's easy to walk-through and debug. I recommend running it in debug mode (I use WebStorm) and checking what each line does :) |
You are missing execSync on line 21: const candidates = cleanArray((`find ${DIR} -type d -name 'node_modules' -prune\`)) This works for me: const candidates = cleanArray(execSync(`find ${DIR} -type d -name 'node_modules' -prune`)) |
@Strajk just tried to give it to go and got an error
I am on the latest Big Sur 11.2.1 UPD: If I just run this command Tried to get manually to this folder, seems like no permissions to get in to folder |
Sorry, it's not clear to me and it hasn't been brought up yet – why not just put all your JS projects into ~/Sites, and drag the Sites directory into the Spotlight privacy pane? Just exclude all your web projects from being Spotlight indexed in general since you can ⌘P in your IDE to find what you want. Then npm/yarn installs won't trigger an mds_stores reindex. |
@jasonbarry However I wonder how to fix the error above 🤪 |
You don't know how many times I've been saved by spotlight searching a snippet of code that I KNEW I had somewhere, but didn't know where. Also, webstorm is much more of an energy hog than spotlight is (after indexing, that is). |
I had the same issue and resolved it by granting |
Base on @chrisbull and @dicksonhk , those find command is still create too mouch file on my macOS Big Sur 11.6. so I modified the command
this command affect folders that have no |
@JamesLIAOHJ I thought apple removed support for |
No, the only way to stop it is to add your whole project folder into "Privacy of spotlight". Evil Apple. |
I use one of the LLMs to complete the shell verion of the script based on @Strajk , and it worked! #!/bin/bash
# Function to exclude a path
exclude_from_spotlight() {
local folder=$1
if [ -d "$folder" ]; then
echo "Excluding: $folder"
# sudo mdutil -i off "$folder" > /dev/null 2>&1
# sudo mdutil -E "$folder" > /dev/null 2>&1
sudo /usr/libexec/PlistBuddy -c "Add :Exclusions: string $folder" /System/Volumes/Data/.Spotlight-V100/VolumeConfiguration.plist
else
echo "Path does not exist or is invalid: $folder"
fi
}
# Function to find top-level node_modules and .git folders
find_top_level_folders() {
local folder_name=$1
local search_path=$2
find "$search_path" -type d -name "$folder_name" -prune 2>/dev/null
}
# Main logic
if [ -z "$1" ]; then
echo "Please enter the folder path to search. Example: ./exclude_spotlight.sh /Users/your_username"
exit 1
fi
search_path=$1
echo "Starting to exclude top-level node_modules and .git folders in path $search_path..."
# Exclude all top-level node_modules folders
find_top_level_folders "node_modules" "$search_path" | while read -r folder; do
exclude_from_spotlight "$folder"
done
# Exclude all top-level .git folders
find_top_level_folders ".git" "$search_path" | while read -r folder; do
exclude_from_spotlight "$folder"
done
echo "All specified folders have been excluded from Spotlight."
Just save the code to ./exclude_spotlight.sh /Users/your_username |
Do you want to request a feature or report a bug?
Feature
What is the current behavior?
node_modules folders are indexed by Spotlight on macOS
What is the expected behavior?
node_modules folders are (possibly optionally) excluded from indexing
This can be done by putting a
.metadata_never_index
in node_modules whenever the folder is created.This may not work (it didn't work for me?)Looks like I was holding it wrong.Another way is to create node_modules.noindex, and symlink node_modules to it. Alternatively, figure out a way to programmatically add to the list of Spotlight exclusions.
The text was updated successfully, but these errors were encountered: