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

6

28

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?

Subscript answered 29/8, 2013 at 13:2 Comment(1)
So what have you tried so far? node-shell seems to have the features, among many others.Shortly
G
26

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!

Gagman answered 29/8, 2013 at 14:38 Comment(4)
I was using the (incorrect) command: rlwrap node and was frustrated as to why the Node REPL wasn't preserving my history. This solved my problem!Brianbriana
Though rlwrap make you loose the pretty node repl colors.Contain
Also the tab completionRephrase
FYI, dreampulse's answer preserves colors and tab completion. :)Calliecalligraphy
S
17

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.

Sirajuddaula answered 3/3, 2014 at 15:31 Comment(2)
This method preserves repl colors and tab completion. Yay!Calliecalligraphy
@SamH Edits should not change or extend the original answer in any way. They are meant only to clarify grammar, spelling, formatting, etc. This is due in part to the fact that edits are approved by the community, not the owner of the post you're editing. If you have suggestions, they belong in comments or your own answer. See: meta.stackexchange.com/questions/11474/…Selfrestraint
N
8

io.js 2.0 includes support for persistent REPL history.

env NODE_REPL_HISTORY_FILE=$HOME/.node_history iojs

The maximum length of the history can be set with NODE_REPL_HISTORY_SIZE, which defaults to 1000.

In io.js 3.0+, REPL history is enabled by default and NODE_REPL_HISTORY_FILE is deprecated in favor of NODE_REPL_HISTORY (default: ~/.node_repl_history).

Newsboy answered 6/5, 2015 at 20:31 Comment(1)
I made an alias for this like @badsyntax: alias iojs='env NODE_REPL_HISTORY_FILE=$HOME/.node_history iojs'Chrysoberyl
U
8

As well check file .node_repl_history in user home directory.

Underlying answered 7/12, 2018 at 8:2 Comment(0)
R
7

Node supports this natively.

When in node REPL just issue the following command to save the history:

$> .save ./file/to/save.js

Reference: Commands and Special Keys

Rejoin answered 22/4, 2018 at 3:20 Comment(0)
C
5

I like the combination of both dreampulse's and badsyntax's answers. With repl.history, plus an addition to my .bash_profile I get the behavior I expect, which is command history and syntax highlighting in the node interactive shell, but bypassing repl when called with arguments (to run a script).

npm install -g repl.history

Then edit your ~/.bash_profile, adding:

function node(){
    if test "$#" -lt 1; then repl.history
    else env node $@; fi; }

Now restart your shell or run . ~/.bash_profile and you're good to go.


Now running

$ node

will open the repl.history nodejs interactive shell, and

$ node program.js [...]

will run program.js with node as expected.

Calliecalligraphy answered 26/1, 2015 at 1:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.