3

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.

technogeek1995
  • 3,185
  • 2
  • 31
  • 52
David Heremans
  • 631
  • 1
  • 5
  • 26
  • How old is your computer ? Can you post your package.json in order to check the modules ? Do you have mounts? What S.O are you using? – filipe Jun 13 '19 at 20:27
  • Are you using docker also? what is the error? You need to add a few examples of the errors when it happens – Tarun Lalwani Jun 15 '19 at 12:03
  • It's a 2015 macbook pro running Mojave (10.14). It were to be hardware related, I would expect error to occur elsewhere as well. Not using docker - just plain and simply fetching npm modules using npm install module --save. – David Heremans Jun 19 '19 at 18:14
  • What is the specific dependency that is causing the issue? Can you add your `package.json` to your question? – technogeek1995 Jun 20 '19 at 12:46
  • Do you have anything at all running that may wind up locking any of the files while npm is trying to run? e.g.: anti-virus software? An IDE with git integration? File-watching build? – Wyck Jun 20 '19 at 16:57
  • Well yes - I jave phpstorm running, but the files WERE there in a previous npm run dev session and randomly get lost... it’s not a specific dependency either - it kust occurs randomly... – David Heremans Jun 23 '19 at 09:28

2 Answers2

0

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.

technogeek1995
  • 3,185
  • 2
  • 31
  • 52
Brhaka
  • 1,622
  • 3
  • 11
  • 31
  • I'm on node v10.10 and npm v6.5. I have no other issues than with npm modules, so I doubt that it is an issue with the hard disk - in that case, it would be more random. And last but not least: the linked answer is another issue - as I stated, the modules re-appear when doing a fresh install, so it's not that they are not correctly saved to package.json. Thanks for responding, but I'm afraid it doesn't help me :( – David Heremans Jun 19 '19 at 18:12
  • 1
    Sorry for that dude, i can't help you... :( – Brhaka Jun 19 '19 at 18:14
0

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
ctietze
  • 2,805
  • 25
  • 46