首页 > 解决方案 > 如何在禁用历史保存的情况下运行 Node.js CLI REPL?

问题描述

我喜欢在 Node.js REPL 中测试我/变得不太熟悉的 JS/ES API,但大多数时候我不希望将任何内容保存到 REPL 历史文件中以供以后参考或研究。

与大多数 shell 不同,Node CLI REPL 也是如此,即使您在命令前加上空格,它也会将其保存到历史记录中。

man node我用and检查了 Node 的手册页node --help,没有什么像--no-save大多数其他 CLI REPL 那样,唯一接近它的是:

Environment variables:
     NODE_REPL_HISTORY file
             Path to the file used to store persistent REPL history.  The default path is ~/.node_repl_history, which is overridden by this variable.  Setting the value to an empty string
             ("" or " ") will disable persistent REPL history.

标签: node.jscommand-lineterminalcommand-line-interfaceread-eval-print-loop

解决方案


您可以在启动节点之前临时设置NODE_REPL_HISTORY为其他内容(例如/dev/null),因此 Node.js cli repl 不会默认保存为默认值~/.node_repl_history

export NODE_REPL_HISTORY="/dev/null" && node

但不要忘记unset NODE_REPL_HISTORY在 Node 退出后,以便下次启动 Node 时通常NODE_REPL_HISTORY默认为默认配置:

export NODE_REPL_HISTORY="/dev/null" && node && unset NODE_REPL_HISTORY

如果你不想每次都输入,你可以nonode在你的 shell 的 rc 文件中设置别名(命令)上面的行:

alias nonode="export NODE_REPL_HISTORY="/dev/null" && node && unset NODE_REPL_HISTORY"


推荐阅读