4

After using node in interactive/command line mode to explore, how can I copy my command history to a text file for my own future records?

The up arrow key recalls previous node statements, and furthermore already persists across logins, so the history is already being saved somewhere in node.js. But where is it saved? How can I copy it?

But I could find nothing in node --help

Paul
  • 26,170
  • 12
  • 85
  • 119
  • Possible duplicate of [Is there a way to have node preserve command line history between sessions?](https://stackoverflow.com/questions/18511787/is-there-a-way-to-have-node-preserve-command-line-history-between-sessions) – Shaunak Apr 22 '18 at 03:17
  • @Shaunak Indeed the `.save` answer is there, but this is not the same question. The other question is about fixing history when it is broken and not saving, or when something is up with the C library that supports readline/history on the machine where node is installed, or some similar thing. – Paul Apr 22 '18 at 03:41

2 Answers2

6

The read-evaluate-print-loop or REPL has a documented list of special nodejs REPL commands, including a save command:

 .save /path/to/save/my/history 

Also, look in ~/.node_repl_history

Paul
  • 26,170
  • 12
  • 85
  • 119
  • 1
    Saved me! I didn't save my work when using a temporary file and I was able to piece it all back together using this `~/.node_repl_history` file. I had to reverse the lines to have it make sense, but it worked. Thanks! – nomadic_squirrel May 14 '19 at 17:38
-2

you just need to run this command

node app.js > log.txt

this command will save all command prompt output to a file

AliLotfi
  • 432
  • 3
  • 14