diff --git a/doc/api/repl.markdown b/doc/api/repl.markdown index 99e9d0e26d8720..ad429dc0f858b6 100644 --- a/doc/api/repl.markdown +++ b/doc/api/repl.markdown @@ -37,7 +37,7 @@ via the following environment variables: - `NODE_REPL_HISTORY` - When a valid path is given, persistent REPL history will be saved to the specified file rather than `.node_repl_history` in the user's home directory. Setting this value to `""` will disable persistent - REPL history. + REPL history. Whitespace will be trimmed from the value. - `NODE_REPL_HISTORY_SIZE` - defaults to `1000`. Controls how many lines of history will be persisted if history is available. Must be a positive number. - `NODE_REPL_MODE` - may be any of `sloppy`, `strict`, or `magic`. Defaults diff --git a/lib/internal/repl.js b/lib/internal/repl.js index e6b41fbdd89b65..371446a83bd4fd 100644 --- a/lib/internal/repl.js +++ b/lib/internal/repl.js @@ -55,15 +55,26 @@ function createRepl(env, opts, cb) { } const repl = REPL.start(opts); - if (opts.terminal && env.NODE_REPL_HISTORY !== '') { + if (opts.terminal) { return setupHistory(repl, env.NODE_REPL_HISTORY, env.NODE_REPL_HISTORY_FILE, cb); } + repl._historyPrev = _replHistoryMessage; cb(null, repl); } function setupHistory(repl, historyPath, oldHistoryPath, ready) { + // Empty string disables persistent history. + + if (typeof historyPath === 'string') + historyPath = historyPath.trim(); + + if (historyPath === '') { + repl._historyPrev = _replHistoryMessage; + return ready(null, repl); + } + if (!historyPath) { try { historyPath = path.join(os.homedir(), '.node_repl_history'); diff --git a/test/parallel/test-repl-persistent-history.js b/test/parallel/test-repl-persistent-history.js index 81e728974f179d..387cef7e97a794 100644 --- a/test/parallel/test-repl-persistent-history.js +++ b/test/parallel/test-repl-persistent-history.js @@ -85,6 +85,11 @@ const tests = [ test: [UP], expected: [prompt, replDisabled, prompt] }, + { + env: { NODE_REPL_HISTORY: ' ' }, + test: [UP], + expected: [prompt, replDisabled, prompt] + }, { env: { NODE_REPL_HISTORY: '', NODE_REPL_HISTORY_FILE: enoentHistoryPath },