Restoring a Node.js Project after a git-reset

Yesterday I did a very stupid thing. Ran git init, git add . and git reset --hard in succession, in a new Node.js project directory. Every modification that I had done before initializing the git repository was gone.

Fortuntately, I was able to restore the files from a Node.js process that had been still running my app. Here’s how I did it.

Node Inspector

Node.js is known to provide a debugging interface. One cool thing people did with this interface is build a Chrome DevTools front-end for it. It’s called Node Inspector. You can view the in-memory resources, set breakpoints, execute expressions step-by-step and inspect variable values.


How to Connect to a Running Node.js Process

Firstly, you need to find the process id:

$ ps aux | grep node
katspaugh       56880   0,0  0,0  2432784    604 s001  S+    2:47     0:00.00 grep node
katspaugh       56865   0,0  0,3  3049188  26828 s002  S+    2:46     0:00.35 node bin/www

The id is in the second column.

Next, send the process a special signal that tells Node.js to start listening for debug RPC:

$ kill -s USR1 56865

You’ll see Node.js reporting back:

Hit SIGUSR1 - starting debugger agent.
debugger listening on port 5858

Finally, install and launch Node Inspector and view the URL it gives you in Chrome or Safari:

$ npm install -g node-inspector
$ node-inspector
Node Inspector v0.7.3
Visit to start debugging.

That’s it! Now you can just copy & paste all your application files, loaded into the Node.js process, from the DevTools resources panel.

Fork me on GitHub