有没有办法让节点之间的会话保持命令行历史logging?
当我从没有参数的命令行运行node
,我input一个交互式shell。 如果我执行一些命令,退出节点,并重新启动节点,向上箭头不会做任何事情(我希望它滚动我的以前的命令)。
有没有办法我可以交互调用节点,以便它会记住我的旧命令?
您可以使用rlwrap将node.js REPL命令存储在历史logging文件中。
首先,安装rlwrap(用apt-get或brew等软件包pipe理器轻松完成)。
然后为节点添加一个别名:
alias node='env NODE_NO_READLINE=1 rlwrap node'
我在OSX上,所以我添加到我的~/.bash_profile
文件的别名,并通过source ~/.bash_profile
重新加载我的bash_profile文件..我很好去!
希望这可以帮助!
我find了一个很好的小项目,解决了这个问题:
使用npm( npm install -g repl.history
) npm install -g repl.history
并在命令行上运行repl.history
。
io.js 2.0包括对持久性REPL历史logging的支持。
env NODE_REPL_HISTORY_FILE=$HOME/.node_history iojs
历史logging的最大长度可以使用NODE_REPL_HISTORY_SIZE
设置,缺省值为1000。
在io.js 3.0+中,默认启用REPL历史logging, NODE_REPL_HISTORY_FILE
不赞成使用NODE_REPL_HISTORY
(默认值: ~/.node_repl_history
)。
我喜欢dreampulse和badsyntax的结合。 使用repl.history,再加上我的.bash_profile,我得到了我期望的行为,即在节点交互式shell中的命令历史logging和语法高亮显示,但是当用参数调用时(运行脚本)绕过repl。
npm install -g repl.history
然后编辑你的〜/ .bash_profile,添加:
function node(){ if test "$#" -lt 1; then repl.history else env node $@; fi; }
现在重新启动你的shell或运行. ~/.bash_profile
. ~/.bash_profile
,你很好去。
现在运行
$ node
将打开repl.history nodejs交互式shell和
$ node program.js [...]
将按预期方式运行program.js节点。