This sounds like a weird issue more about data loss than npm.
To investigate, I suggest you watch the folder tree for changes and display an alert when stuff is deleted. Then at least you can narrow down when this happens. Maybe it's not even during development, but some everyday service, of a clean-up tool.
Try a built-in tool: Launch "Folder Actions Setup" (from Spotlight) or right-click your packages folder and select Services > Folder Actions Setup ... to configure a script to run on folder changes in your Mac. If it doesn't trigger on sub-directory changes, I'm afraid you'd need to ...
Use a command-line tool like fswatch:
$ brew install fswatch
$ fswatch --recursive --timestamp /path/to/modules
You could filter by event types (see fswatch docs), but pay attention you'll need both --event Removed
and --event Renamed
because using the Finder Trash is really a rename/move command.
Also see how to use fswatch
and other CLI tools for Mac here: Is there a command like "watch" or "inotifywait" on the Mac?
In the case of fswatch, there is no on-change trigger for custom scripts. You'd rather need to create a shell script that runs until you kill it, and configure fswatch to exit after 1 event already to process the output immediately:
$WATCHED_PATH="/path/to/node/modules"
while [[ true ]]
do
path=`fswatch --recursive --one-event --event Removed --event Renamed "$WATCHED_PATH"`
applescript="tell app \"System Events\" to display dialog \"Removed $path\""
osascript -e $applescript
done