Node modules go missing without any reason on my Mac

Viewed 903

I have a dozen dependencies in my package.json file with those pulling in many other dependencies. We all know the dependency tree can grow very fast.

I run npm run dev a number of times without it giving me an error. However, it recently started to complain about a missing module. There is no delta in the dependency tree at those times, nor did we change any code that might need the missing module.

Clearing the node_modules folder and doing a fresh npm install always fixes the issue, but I don't know what the root cause is or how to remedy. In the past, it was an issue that I ran into occasionally. However, it starts to appear more frequently.

2 Answers

This might be a hardware issue with the hard drive. If your drive is too old, it could corrupt, delete, or send files into limbo.

However, if you find that the issue isn't related to the hardware. There is a github issue for a similar problem here. However, updating npm to 5.7.1 seemed to resolve that.

Read this guide to update npm. If you can't, use npm install --save.

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.

  1. 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 ...

  2. 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
Related