Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to have node preserve command line history between sessions?

Tags:

When I run node from the command line with no arguments, I enter an interactive shell. If I execute some commands, exit node, and restart node, the up arrow doesn't do anything (I'd like it scroll through my previous commands).

Is there a way I can invoke node interactively such that it will remember my old commands?

like image 285
Brian Avatar asked Aug 29 '13 13:08

Brian


People also ask

How do I keep bash history?

bash_history exists. If it does not, there's nowhere for bash to save command history. this just creates the empty . bash_history file in your home directory, which will fill up with your commands, and should persist between sessions.

How do I run node js files forever?

Installing forever Forever can be used in two ways: with the forever CLI tool and with forever-monitor. The forever-monitor tool can be used to run an application with forever using code.

What is REPL node JS?

The Node. js Read-Eval-Print-Loop (REPL) is an interactive shell that processes Node. js expressions. The shell reads JavaScript code the user enters, evaluates the result of interpreting the line of code, prints the result to the user, and loops until the user signals to quit. The REPL is bundled with every Node.


2 Answers

You could use rlwrap to store node.js REPL commands in a history file.

First, install rlwrap (done easily with a package manager like apt-get or brew etc).

Then add an alias for node:

alias node='env NODE_NO_READLINE=1 rlwrap node' 

I'm on OSX so I add that alias to my ~/.bash_profile file, and would reload my bash_profile file via source ~/.bash_profile.. and I'm good to go!

Hope this helps!

like image 93
badsyntax Avatar answered Sep 18 '22 16:09

badsyntax


I found a nice little project, which solves the problem:

https://www.npmjs.org/package/repl.history

install using npm (npm install -g repl.history) and run repl.history on the command line.

like image 26
dreampulse Avatar answered Sep 20 '22 16:09

dreampulse