/usr/share/doc/nodejs-docs/doc/api
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <meta name="nodejs.org:node-version" content="v16.20.2"> <title>REPL | Node.js v16.20.2 Documentation</title> <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback"> <link rel="stylesheet" href="assets/style.css"> <link rel="stylesheet" href="assets/hljs.css"> <link rel="canonical" href="https://nodejs.org/api/repl.html"> <script async defer src="assets/api.js" type="text/javascript"></script> </head> <body class="alt apidoc" id="api-section-repl"> <div id="content" class="clearfix"> <div id="column2" class="interior"> <div id="intro" class="interior"> <a href="/" title="Go back to the home page"> Node.js </a> </div> <ul> <li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> <li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> </ul> <hr class="line"> <ul> <li><a href="assert.html" class="nav-assert">Assertion testing</a></li> <li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> <li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> <li><a href="buffer.html" class="nav-buffer">Buffer</a></li> <li><a href="addons.html" class="nav-addons">C++ addons</a></li> <li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> <li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> <li><a href="child_process.html" class="nav-child_process">Child processes</a></li> <li><a href="cluster.html" class="nav-cluster">Cluster</a></li> <li><a href="cli.html" class="nav-cli">Command-line options</a></li> <li><a href="console.html" class="nav-console">Console</a></li> <li><a href="corepack.html" class="nav-corepack">Corepack</a></li> <li><a href="crypto.html" class="nav-crypto">Crypto</a></li> <li><a href="debugger.html" class="nav-debugger">Debugger</a></li> <li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> <li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> <li><a href="dns.html" class="nav-dns">DNS</a></li> <li><a href="domain.html" class="nav-domain">Domain</a></li> <li><a href="errors.html" class="nav-errors">Errors</a></li> <li><a href="events.html" class="nav-events">Events</a></li> <li><a href="fs.html" class="nav-fs">File system</a></li> <li><a href="globals.html" class="nav-globals">Globals</a></li> <li><a href="http.html" class="nav-http">HTTP</a></li> <li><a href="http2.html" class="nav-http2">HTTP/2</a></li> <li><a href="https.html" class="nav-https">HTTPS</a></li> <li><a href="inspector.html" class="nav-inspector">Inspector</a></li> <li><a href="intl.html" class="nav-intl">Internationalization</a></li> <li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> <li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> <li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> <li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> <li><a href="net.html" class="nav-net">Net</a></li> <li><a href="os.html" class="nav-os">OS</a></li> <li><a href="path.html" class="nav-path">Path</a></li> <li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> <li><a href="permissions.html" class="nav-permissions">Permissions</a></li> <li><a href="process.html" class="nav-process">Process</a></li> <li><a href="punycode.html" class="nav-punycode">Punycode</a></li> <li><a href="querystring.html" class="nav-querystring">Query strings</a></li> <li><a href="readline.html" class="nav-readline">Readline</a></li> <li><a href="repl.html" class="nav-repl active">REPL</a></li> <li><a href="report.html" class="nav-report">Report</a></li> <li><a href="stream.html" class="nav-stream">Stream</a></li> <li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> <li><a href="test.html" class="nav-test">Test runner</a></li> <li><a href="timers.html" class="nav-timers">Timers</a></li> <li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> <li><a href="tracing.html" class="nav-tracing">Trace events</a></li> <li><a href="tty.html" class="nav-tty">TTY</a></li> <li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> <li><a href="url.html" class="nav-url">URL</a></li> <li><a href="util.html" class="nav-util">Utilities</a></li> <li><a href="v8.html" class="nav-v8">V8</a></li> <li><a href="vm.html" class="nav-vm">VM</a></li> <li><a href="wasi.html" class="nav-wasi">WASI</a></li> <li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> <li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> <li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> <li><a href="zlib.html" class="nav-zlib">Zlib</a></li> </ul> <hr class="line"> <ul> <li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> </ul> </div> <div id="column1" data-id="repl" class="interior"> <header class="header"> <div class="header-container"> <h1>Node.js v16.20.2 documentation</h1> <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden> <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24"> <path fill="none" d="M0 0h24v24H0z" /> <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/> <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/> </svg> <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24"> <path d="M0 0h24v24H0z" fill="none" /> <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/> </svg> </button> </div> <div id="gtoc"> <ul> <li class="pinned-header">Node.js v16.20.2</li> <li class="picker-header"> <a href="#"> <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> Table of contents </a> <div class="picker"><div class="toc"><ul> <li><span class="stability_2"><a href="#repl">REPL</a></span> <ul> <li><a href="#design-and-features">Design and features</a> <ul> <li><a href="#commands-and-special-keys">Commands and special keys</a></li> <li><a href="#default-evaluation">Default evaluation</a> <ul> <li><a href="#javascript-expressions">JavaScript expressions</a></li> <li><a href="#global-and-local-scope">Global and local scope</a></li> <li><a href="#accessing-core-nodejs-modules">Accessing core Node.js modules</a></li> <li><a href="#global-uncaught-exceptions">Global uncaught exceptions</a></li> <li><a href="#assignment-of-the-_-underscore-variable">Assignment of the <code>_</code> (underscore) variable</a></li> <li><a href="#await-keyword"><code>await</code> keyword</a></li> </ul> </li> <li><a href="#reverse-i-search">Reverse-i-search</a></li> <li><a href="#custom-evaluation-functions">Custom evaluation functions</a> <ul> <li><a href="#recoverable-errors">Recoverable errors</a></li> </ul> </li> <li><a href="#customizing-repl-output">Customizing REPL output</a></li> </ul> </li> <li><a href="#class-replserver">Class: <code>REPLServer</code></a> <ul> <li><a href="#event-exit">Event: <code>'exit'</code></a></li> <li><a href="#event-reset">Event: <code>'reset'</code></a></li> <li><a href="#replserverdefinecommandkeyword-cmd"><code>replServer.defineCommand(keyword, cmd)</code></a></li> <li><a href="#replserverdisplaypromptpreservecursor"><code>replServer.displayPrompt([preserveCursor])</code></a></li> <li><a href="#replserverclearbufferedcommand"><code>replServer.clearBufferedCommand()</code></a></li> <li><span class="stability_0"><a href="#replserverparsereplkeywordkeyword-rest"><code>replServer.parseREPLKeyword(keyword[, rest])</code></a></span></li> <li><a href="#replserversetuphistoryhistorypath-callback"><code>replServer.setupHistory(historyPath, callback)</code></a></li> </ul> </li> <li><a href="#replbuiltinmodules"><code>repl.builtinModules</code></a></li> <li><a href="#replstartoptions"><code>repl.start([options])</code></a></li> <li><a href="#the-nodejs-repl">The Node.js REPL</a> <ul> <li><a href="#environment-variable-options">Environment variable options</a></li> <li><a href="#persistent-history">Persistent history</a></li> <li><a href="#using-the-nodejs-repl-with-advanced-line-editors">Using the Node.js REPL with advanced line-editors</a></li> <li><a href="#starting-multiple-repl-instances-against-a-single-running-instance">Starting multiple REPL instances against a single running instance</a></li> </ul> </li> </ul> </li> </ul></div></div> </li> <li class="picker-header"> <a href="#"> <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> Index </a> <div class="picker"><ul> <li><a href="documentation.html" class="nav-documentation">About this documentation</a></li> <li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li> <li> <a href="index.html">Index</a> </li> </ul> <hr class="line"> <ul> <li><a href="assert.html" class="nav-assert">Assertion testing</a></li> <li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li> <li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li> <li><a href="buffer.html" class="nav-buffer">Buffer</a></li> <li><a href="addons.html" class="nav-addons">C++ addons</a></li> <li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li> <li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li> <li><a href="child_process.html" class="nav-child_process">Child processes</a></li> <li><a href="cluster.html" class="nav-cluster">Cluster</a></li> <li><a href="cli.html" class="nav-cli">Command-line options</a></li> <li><a href="console.html" class="nav-console">Console</a></li> <li><a href="corepack.html" class="nav-corepack">Corepack</a></li> <li><a href="crypto.html" class="nav-crypto">Crypto</a></li> <li><a href="debugger.html" class="nav-debugger">Debugger</a></li> <li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li> <li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li> <li><a href="dns.html" class="nav-dns">DNS</a></li> <li><a href="domain.html" class="nav-domain">Domain</a></li> <li><a href="errors.html" class="nav-errors">Errors</a></li> <li><a href="events.html" class="nav-events">Events</a></li> <li><a href="fs.html" class="nav-fs">File system</a></li> <li><a href="globals.html" class="nav-globals">Globals</a></li> <li><a href="http.html" class="nav-http">HTTP</a></li> <li><a href="http2.html" class="nav-http2">HTTP/2</a></li> <li><a href="https.html" class="nav-https">HTTPS</a></li> <li><a href="inspector.html" class="nav-inspector">Inspector</a></li> <li><a href="intl.html" class="nav-intl">Internationalization</a></li> <li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li> <li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li> <li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li> <li><a href="packages.html" class="nav-packages">Modules: Packages</a></li> <li><a href="net.html" class="nav-net">Net</a></li> <li><a href="os.html" class="nav-os">OS</a></li> <li><a href="path.html" class="nav-path">Path</a></li> <li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li> <li><a href="permissions.html" class="nav-permissions">Permissions</a></li> <li><a href="process.html" class="nav-process">Process</a></li> <li><a href="punycode.html" class="nav-punycode">Punycode</a></li> <li><a href="querystring.html" class="nav-querystring">Query strings</a></li> <li><a href="readline.html" class="nav-readline">Readline</a></li> <li><a href="repl.html" class="nav-repl active">REPL</a></li> <li><a href="report.html" class="nav-report">Report</a></li> <li><a href="stream.html" class="nav-stream">Stream</a></li> <li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li> <li><a href="test.html" class="nav-test">Test runner</a></li> <li><a href="timers.html" class="nav-timers">Timers</a></li> <li><a href="tls.html" class="nav-tls">TLS/SSL</a></li> <li><a href="tracing.html" class="nav-tracing">Trace events</a></li> <li><a href="tty.html" class="nav-tty">TTY</a></li> <li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li> <li><a href="url.html" class="nav-url">URL</a></li> <li><a href="util.html" class="nav-util">Utilities</a></li> <li><a href="v8.html" class="nav-v8">V8</a></li> <li><a href="vm.html" class="nav-vm">VM</a></li> <li><a href="wasi.html" class="nav-wasi">WASI</a></li> <li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li> <li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li> <li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li> <li><a href="zlib.html" class="nav-zlib">Zlib</a></li> </ul> <hr class="line"> <ul> <li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li> </ul></div> </li> <li class="picker-header"> <a href="#"> <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> Other versions </a> <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v20.x/api/repl.html">20.x</a></li> <li><a href="https://nodejs.org/docs/latest-v19.x/api/repl.html">19.x</a></li> <li><a href="https://nodejs.org/docs/latest-v18.x/api/repl.html">18.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v17.x/api/repl.html">17.x</a></li> <li><a href="https://nodejs.org/docs/latest-v16.x/api/repl.html">16.x <b>LTS</b></a></li> <li><a href="https://nodejs.org/docs/latest-v15.x/api/repl.html">15.x</a></li> <li><a href="https://nodejs.org/docs/latest-v14.x/api/repl.html">14.x</a></li> <li><a href="https://nodejs.org/docs/latest-v13.x/api/repl.html">13.x</a></li> <li><a href="https://nodejs.org/docs/latest-v12.x/api/repl.html">12.x</a></li> <li><a href="https://nodejs.org/docs/latest-v11.x/api/repl.html">11.x</a></li> <li><a href="https://nodejs.org/docs/latest-v10.x/api/repl.html">10.x</a></li> <li><a href="https://nodejs.org/docs/latest-v9.x/api/repl.html">9.x</a></li> <li><a href="https://nodejs.org/docs/latest-v8.x/api/repl.html">8.x</a></li> <li><a href="https://nodejs.org/docs/latest-v7.x/api/repl.html">7.x</a></li> <li><a href="https://nodejs.org/docs/latest-v6.x/api/repl.html">6.x</a></li> <li><a href="https://nodejs.org/docs/latest-v5.x/api/repl.html">5.x</a></li> <li><a href="https://nodejs.org/docs/latest-v4.x/api/repl.html">4.x</a></li> <li><a href="https://nodejs.org/docs/latest-v0.12.x/api/repl.html">0.12.x</a></li> <li><a href="https://nodejs.org/docs/latest-v0.10.x/api/repl.html">0.10.x</a></li></ol></div> </li> <li class="picker-header"> <a href="#"> <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> Options </a> <div class="picker"> <ul> <li> <a href="all.html">View on single page</a> </li> <li> <a href="repl.json">View as JSON</a> </li> <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/repl.md">Edit on GitHub</a></li> </ul> </div> </li> </ul> </div> <hr> </header> <details id="toc" open><summary>Table of contents</summary><ul> <li><span class="stability_2"><a href="#repl">REPL</a></span> <ul> <li><a href="#design-and-features">Design and features</a> <ul> <li><a href="#commands-and-special-keys">Commands and special keys</a></li> <li><a href="#default-evaluation">Default evaluation</a> <ul> <li><a href="#javascript-expressions">JavaScript expressions</a></li> <li><a href="#global-and-local-scope">Global and local scope</a></li> <li><a href="#accessing-core-nodejs-modules">Accessing core Node.js modules</a></li> <li><a href="#global-uncaught-exceptions">Global uncaught exceptions</a></li> <li><a href="#assignment-of-the-_-underscore-variable">Assignment of the <code>_</code> (underscore) variable</a></li> <li><a href="#await-keyword"><code>await</code> keyword</a></li> </ul> </li> <li><a href="#reverse-i-search">Reverse-i-search</a></li> <li><a href="#custom-evaluation-functions">Custom evaluation functions</a> <ul> <li><a href="#recoverable-errors">Recoverable errors</a></li> </ul> </li> <li><a href="#customizing-repl-output">Customizing REPL output</a></li> </ul> </li> <li><a href="#class-replserver">Class: <code>REPLServer</code></a> <ul> <li><a href="#event-exit">Event: <code>'exit'</code></a></li> <li><a href="#event-reset">Event: <code>'reset'</code></a></li> <li><a href="#replserverdefinecommandkeyword-cmd"><code>replServer.defineCommand(keyword, cmd)</code></a></li> <li><a href="#replserverdisplaypromptpreservecursor"><code>replServer.displayPrompt([preserveCursor])</code></a></li> <li><a href="#replserverclearbufferedcommand"><code>replServer.clearBufferedCommand()</code></a></li> <li><span class="stability_0"><a href="#replserverparsereplkeywordkeyword-rest"><code>replServer.parseREPLKeyword(keyword[, rest])</code></a></span></li> <li><a href="#replserversetuphistoryhistorypath-callback"><code>replServer.setupHistory(historyPath, callback)</code></a></li> </ul> </li> <li><a href="#replbuiltinmodules"><code>repl.builtinModules</code></a></li> <li><a href="#replstartoptions"><code>repl.start([options])</code></a></li> <li><a href="#the-nodejs-repl">The Node.js REPL</a> <ul> <li><a href="#environment-variable-options">Environment variable options</a></li> <li><a href="#persistent-history">Persistent history</a></li> <li><a href="#using-the-nodejs-repl-with-advanced-line-editors">Using the Node.js REPL with advanced line-editors</a></li> <li><a href="#starting-multiple-repl-instances-against-a-single-running-instance">Starting multiple REPL instances against a single running instance</a></li> </ul> </li> </ul> </li> </ul></details> <div id="apicontent"> <h2>REPL<span><a class="mark" href="#repl" id="repl">#</a></span><a aria-hidden="true" class="legacy" id="repl_repl"></a></h2> <p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> <p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v16.20.2/lib/repl.js">lib/repl.js</a></p> <p>The <code>node:repl</code> module provides a Read-Eval-Print-Loop (REPL) implementation that is available both as a standalone program or includible in other applications. It can be accessed using:</p> <pre><code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:repl'</span>);</code></pre> <section><h3>Design and features<span><a class="mark" href="#design-and-features" id="design-and-features">#</a></span><a aria-hidden="true" class="legacy" id="repl_design_and_features"></a></h3> <p>The <code>node:repl</code> module exports the <a href="#class-replserver"><code>repl.REPLServer</code></a> class. While running, instances of <a href="#class-replserver"><code>repl.REPLServer</code></a> will accept individual lines of user input, evaluate those according to a user-defined evaluation function, then output the result. Input and output may be from <code>stdin</code> and <code>stdout</code>, respectively, or may be connected to any Node.js <a href="stream.html">stream</a>.</p> <p>Instances of <a href="#class-replserver"><code>repl.REPLServer</code></a> support automatic completion of inputs, completion preview, simplistic Emacs-style line editing, multi-line inputs, <a href="https://en.wikipedia.org/wiki/Z_shell">ZSH</a>-like reverse-i-search, <a href="https://en.wikipedia.org/wiki/Z_shell">ZSH</a>-like substring-based history search, ANSI-styled output, saving and restoring current REPL session state, error recovery, and customizable evaluation functions. Terminals that do not support ANSI styles and Emacs-style line editing automatically fall back to a limited feature set.</p> <h4>Commands and special keys<span><a class="mark" href="#commands-and-special-keys" id="commands-and-special-keys">#</a></span><a aria-hidden="true" class="legacy" id="repl_commands_and_special_keys"></a></h4> <p>The following special commands are supported by all REPL instances:</p> <ul> <li><code>.break</code>: When in the process of inputting a multi-line expression, enter the <code>.break</code> command (or press <kbd>Ctrl</kbd>+<kbd>C</kbd>) to abort further input or processing of that expression.</li> <li><code>.clear</code>: Resets the REPL <code>context</code> to an empty object and clears any multi-line expression being input.</li> <li><code>.exit</code>: Close the I/O stream, causing the REPL to exit.</li> <li><code>.help</code>: Show this list of special commands.</li> <li><code>.save</code>: Save the current REPL session to a file: <code>> .save ./file/to/save.js</code></li> <li><code>.load</code>: Load a file into the current REPL session. <code>> .load ./file/to/load.js</code></li> <li><code>.editor</code>: Enter editor mode (<kbd>Ctrl</kbd>+<kbd>D</kbd> to finish, <kbd>Ctrl</kbd>+<kbd>C</kbd> to cancel).</li> </ul> <pre><code class="language-console"><span class="hljs-meta prompt_">> </span><span class="language-bash">.editor</span> // Entering editor mode (^D to finish, ^C to cancel) function welcome(name) { return `Hello ${name}!`; } welcome('Node.js User'); // ^D 'Hello Node.js User!' <span class="hljs-meta prompt_">></span></code></pre> <p>The following key combinations in the REPL have these special effects:</p> <ul> <li><kbd>Ctrl</kbd>+<kbd>C</kbd>: When pressed once, has the same effect as the <code>.break</code> command. When pressed twice on a blank line, has the same effect as the <code>.exit</code> command.</li> <li><kbd>Ctrl</kbd>+<kbd>D</kbd>: Has the same effect as the <code>.exit</code> command.</li> <li><kbd>Tab</kbd>: When pressed on a blank line, displays global and local (scope) variables. When pressed while entering other input, displays relevant autocompletion options.</li> </ul> <p>For key bindings related to the reverse-i-search, see <a href="#reverse-i-search"><code>reverse-i-search</code></a>. For all other key bindings, see <a href="readline.html#tty-keybindings">TTY keybindings</a>.</p> <h4>Default evaluation<span><a class="mark" href="#default-evaluation" id="default-evaluation">#</a></span><a aria-hidden="true" class="legacy" id="repl_default_evaluation"></a></h4> <p>By default, all instances of <a href="#class-replserver"><code>repl.REPLServer</code></a> use an evaluation function that evaluates JavaScript expressions and provides access to Node.js built-in modules. This default behavior can be overridden by passing in an alternative evaluation function when the <a href="#class-replserver"><code>repl.REPLServer</code></a> instance is created.</p> <h5>JavaScript expressions<span><a class="mark" href="#javascript-expressions" id="javascript-expressions">#</a></span><a aria-hidden="true" class="legacy" id="repl_javascript_expressions"></a></h5> <p>The default evaluator supports direct evaluation of JavaScript expressions:</p> <pre><code class="language-console"><span class="hljs-meta prompt_">> </span><span class="language-bash">1 + 1</span> 2 <span class="hljs-meta prompt_">> </span><span class="language-bash">const m = 2</span> undefined <span class="hljs-meta prompt_">> </span><span class="language-bash">m + 1</span> 3</code></pre> <p>Unless otherwise scoped within blocks or functions, variables declared either implicitly or using the <code>const</code>, <code>let</code>, or <code>var</code> keywords are declared at the global scope.</p> <h5>Global and local scope<span><a class="mark" href="#global-and-local-scope" id="global-and-local-scope">#</a></span><a aria-hidden="true" class="legacy" id="repl_global_and_local_scope"></a></h5> <p>The default evaluator provides access to any variables that exist in the global scope. It is possible to expose a variable to the REPL explicitly by assigning it to the <code>context</code> object associated with each <code>REPLServer</code>:</p> <pre><code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:repl'</span>); <span class="hljs-keyword">const</span> msg = <span class="hljs-string">'message'</span>; repl.<span class="hljs-title function_">start</span>(<span class="hljs-string">'> '</span>).<span class="hljs-property">context</span>.<span class="hljs-property">m</span> = msg;</code></pre> <p>Properties in the <code>context</code> object appear as local within the REPL:</p> <pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node repl_test.js</span> <span class="hljs-meta prompt_">> </span><span class="language-bash">m</span> 'message'</code></pre> <p>Context properties are not read-only by default. To specify read-only globals, context properties must be defined using <code>Object.defineProperty()</code>:</p> <pre><code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:repl'</span>); <span class="hljs-keyword">const</span> msg = <span class="hljs-string">'message'</span>; <span class="hljs-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>(<span class="hljs-string">'> '</span>); <span class="hljs-title class_">Object</span>.<span class="hljs-title function_">defineProperty</span>(r.<span class="hljs-property">context</span>, <span class="hljs-string">'m'</span>, { <span class="hljs-attr">configurable</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">enumerable</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">value</span>: msg });</code></pre> <h5>Accessing core Node.js modules<span><a class="mark" href="#accessing-core-nodejs-modules" id="accessing-core-nodejs-modules">#</a></span><a aria-hidden="true" class="legacy" id="repl_accessing_core_node_js_modules"></a></h5> <p>The default evaluator will automatically load Node.js core modules into the REPL environment when used. For instance, unless otherwise declared as a global or scoped variable, the input <code>fs</code> will be evaluated on-demand as <code>global.fs = require('node:fs')</code>.</p> <pre><code class="language-console"><span class="hljs-meta prompt_">> </span><span class="language-bash">fs.createReadStream(<span class="hljs-string">'./some/file'</span>);</span></code></pre> <h5>Global uncaught exceptions<span><a class="mark" href="#global-uncaught-exceptions" id="global-uncaught-exceptions">#</a></span><a aria-hidden="true" class="legacy" id="repl_global_uncaught_exceptions"></a></h5> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v12.3.0</td> <td><p>The <code>'uncaughtException'</code> event is from now on triggered if the repl is used as standalone program.</p></td></tr> </tbody></table> </details> </div> <p>The REPL uses the <a href="domain.html"><code>domain</code></a> module to catch all uncaught exceptions for that REPL session.</p> <p>This use of the <a href="domain.html"><code>domain</code></a> module in the REPL has these side effects:</p> <ul> <li> <p>Uncaught exceptions only emit the <a href="process.html#event-uncaughtexception"><code>'uncaughtException'</code></a> event in the standalone REPL. Adding a listener for this event in a REPL within another Node.js program results in <a href="errors.html#err_invalid_repl_input"><code>ERR_INVALID_REPL_INPUT</code></a>.</p> <pre><code class="language-js"><span class="hljs-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>(); r.<span class="hljs-title function_">write</span>(<span class="hljs-string">'process.on("uncaughtException", () => console.log("Foobar"));\n'</span>); <span class="hljs-comment">// Output stream includes:</span> <span class="hljs-comment">// TypeError [ERR_INVALID_REPL_INPUT]: Listeners for `uncaughtException`</span> <span class="hljs-comment">// cannot be used in the REPL</span> r.<span class="hljs-title function_">close</span>();</code></pre> </li> <li> <p>Trying to use <a href="process.html#processsetuncaughtexceptioncapturecallbackfn"><code>process.setUncaughtExceptionCaptureCallback()</code></a> throws an <a href="errors.html#err_domain_cannot_set_uncaught_exception_capture"><code>ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE</code></a> error.</p> </li> </ul> <h5>Assignment of the <code>_</code> (underscore) variable<span><a class="mark" href="#assignment-of-the-_-underscore-variable" id="assignment-of-the-_-underscore-variable">#</a></span><a aria-hidden="true" class="legacy" id="repl_assignment_of_the_underscore_variable"></a></h5> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v9.8.0</td> <td><p>Added <code>_error</code> support.</p></td></tr> </tbody></table> </details> </div> <p>The default evaluator will, by default, assign the result of the most recently evaluated expression to the special variable <code>_</code> (underscore). Explicitly setting <code>_</code> to a value will disable this behavior.</p> <pre><code class="language-console"><span class="hljs-meta prompt_">> </span><span class="language-bash">[ <span class="hljs-string">'a'</span>, <span class="hljs-string">'b'</span>, <span class="hljs-string">'c'</span> ]</span> [ 'a', 'b', 'c' ] <span class="hljs-meta prompt_">> </span><span class="language-bash">_.length</span> 3 <span class="hljs-meta prompt_">> </span><span class="language-bash">_ += 1</span> Expression assignment to _ now disabled. 4 <span class="hljs-meta prompt_">> </span><span class="language-bash">1 + 1</span> 2 <span class="hljs-meta prompt_">> </span><span class="language-bash">_</span> 4</code></pre> <p>Similarly, <code>_error</code> will refer to the last seen error, if there was any. Explicitly setting <code>_error</code> to a value will disable this behavior.</p> <pre><code class="language-console"><span class="hljs-meta prompt_">> </span><span class="language-bash">throw new Error(<span class="hljs-string">'foo'</span>);</span> Error: foo <span class="hljs-meta prompt_">> </span><span class="language-bash">_error.message</span> 'foo'</code></pre> <h5><code>await</code> keyword<span><a class="mark" href="#await-keyword" id="await-keyword">#</a></span><a aria-hidden="true" class="legacy" id="repl_await_keyword"></a></h5> <p>Support for the <code>await</code> keyword is enabled at the top level.</p> <pre><code class="language-console"><span class="hljs-meta prompt_">> </span><span class="language-bash">await Promise.resolve(123)</span> 123 <span class="hljs-meta prompt_">> </span><span class="language-bash">await Promise.reject(new Error(<span class="hljs-string">'REPL await'</span>))</span> Error: REPL await at repl:1:45 <span class="hljs-meta prompt_">> </span><span class="language-bash">const <span class="hljs-built_in">timeout</span> = util.promisify(setTimeout);</span> undefined <span class="hljs-meta prompt_">> </span><span class="language-bash">const old = Date.now(); await <span class="hljs-built_in">timeout</span>(1000); console.log(Date.now() - old);</span> 1002 undefined</code></pre> <p>One known limitation of using the <code>await</code> keyword in the REPL is that it will invalidate the lexical scoping of the <code>const</code> and <code>let</code> keywords.</p> <p>For example:</p> <pre><code class="language-console"><span class="hljs-meta prompt_">> </span><span class="language-bash">const m = await Promise.resolve(123)</span> undefined <span class="hljs-meta prompt_">> </span><span class="language-bash">m</span> 123 <span class="hljs-meta prompt_">> </span><span class="language-bash">const m = await Promise.resolve(234)</span> undefined <span class="hljs-meta prompt_">> </span><span class="language-bash">m</span> 234</code></pre> <p><a href="cli.html#--no-experimental-repl-await"><code>--no-experimental-repl-await</code></a> shall disable top-level await in REPL.</p> <h4>Reverse-i-search<span><a class="mark" href="#reverse-i-search" id="reverse-i-search">#</a></span><a aria-hidden="true" class="legacy" id="repl_reverse_i_search"></a></h4> <div class="api_metadata"> <span>Added in: v13.6.0, v12.17.0</span> </div> <p>The REPL supports bi-directional reverse-i-search similar to <a href="https://en.wikipedia.org/wiki/Z_shell">ZSH</a>. It is triggered with <kbd>Ctrl</kbd>+<kbd>R</kbd> to search backward and <kbd>Ctrl</kbd>+<kbd>S</kbd> to search forwards.</p> <p>Duplicated history entries will be skipped.</p> <p>Entries are accepted as soon as any key is pressed that doesn't correspond with the reverse search. Cancelling is possible by pressing <kbd>Esc</kbd> or <kbd>Ctrl</kbd>+<kbd>C</kbd>.</p> <p>Changing the direction immediately searches for the next entry in the expected direction from the current position on.</p> <h4>Custom evaluation functions<span><a class="mark" href="#custom-evaluation-functions" id="custom-evaluation-functions">#</a></span><a aria-hidden="true" class="legacy" id="repl_custom_evaluation_functions"></a></h4> <p>When a new <a href="#class-replserver"><code>repl.REPLServer</code></a> is created, a custom evaluation function may be provided. This can be used, for instance, to implement fully customized REPL applications.</p> <p>The following illustrates a hypothetical example of a REPL that performs translation of text from one language to another:</p> <pre><code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:repl'</span>); <span class="hljs-keyword">const</span> { <span class="hljs-title class_">Translator</span> } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'translator'</span>); <span class="hljs-keyword">const</span> myTranslator = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Translator</span>(<span class="hljs-string">'en'</span>, <span class="hljs-string">'fr'</span>); <span class="hljs-keyword">function</span> <span class="hljs-title function_">myEval</span>(<span class="hljs-params">cmd, context, filename, callback</span>) { <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, myTranslator.<span class="hljs-title function_">translate</span>(cmd)); } repl.<span class="hljs-title function_">start</span>({ <span class="hljs-attr">prompt</span>: <span class="hljs-string">'> '</span>, <span class="hljs-attr">eval</span>: myEval });</code></pre> <h5>Recoverable errors<span><a class="mark" href="#recoverable-errors" id="recoverable-errors">#</a></span><a aria-hidden="true" class="legacy" id="repl_recoverable_errors"></a></h5> <p>At the REPL prompt, pressing <kbd>Enter</kbd> sends the current line of input to the <code>eval</code> function. In order to support multi-line input, the <code>eval</code> function can return an instance of <code>repl.Recoverable</code> to the provided callback function:</p> <pre><code class="language-js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">myEval</span>(<span class="hljs-params">cmd, context, filename, callback</span>) { <span class="hljs-keyword">let</span> result; <span class="hljs-keyword">try</span> { result = vm.<span class="hljs-title function_">runInThisContext</span>(cmd); } <span class="hljs-keyword">catch</span> (e) { <span class="hljs-keyword">if</span> (<span class="hljs-title function_">isRecoverableError</span>(e)) { <span class="hljs-keyword">return</span> <span class="hljs-title function_">callback</span>(<span class="hljs-keyword">new</span> repl.<span class="hljs-title class_">Recoverable</span>(e)); } } <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, result); } <span class="hljs-keyword">function</span> <span class="hljs-title function_">isRecoverableError</span>(<span class="hljs-params">error</span>) { <span class="hljs-keyword">if</span> (error.<span class="hljs-property">name</span> === <span class="hljs-string">'SyntaxError'</span>) { <span class="hljs-keyword">return</span> <span class="hljs-regexp">/^(Unexpected end of input|Unexpected token)/</span>.<span class="hljs-title function_">test</span>(error.<span class="hljs-property">message</span>); } <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>; }</code></pre> <h4>Customizing REPL output<span><a class="mark" href="#customizing-repl-output" id="customizing-repl-output">#</a></span><a aria-hidden="true" class="legacy" id="repl_customizing_repl_output"></a></h4> <p>By default, <a href="#class-replserver"><code>repl.REPLServer</code></a> instances format output using the <a href="util.html#utilinspectobject-options"><code>util.inspect()</code></a> method before writing the output to the provided <code>Writable</code> stream (<code>process.stdout</code> by default). The <code>showProxy</code> inspection option is set to true by default and the <code>colors</code> option is set to true depending on the REPL's <code>useColors</code> option.</p> <p>The <code>useColors</code> boolean option can be specified at construction to instruct the default writer to use ANSI style codes to colorize the output from the <code>util.inspect()</code> method.</p> <p>If the REPL is run as standalone program, it is also possible to change the REPL's <a href="util.html#utilinspectobject-options">inspection defaults</a> from inside the REPL by using the <code>inspect.replDefaults</code> property which mirrors the <code>defaultOptions</code> from <a href="util.html#utilinspectobject-options"><code>util.inspect()</code></a>.</p> <pre><code class="language-console"><span class="hljs-meta prompt_">> </span><span class="language-bash">util.inspect.replDefaults.compact = <span class="hljs-literal">false</span>;</span> false <span class="hljs-meta prompt_">> </span><span class="language-bash">[1]</span> [ 1 ] <span class="hljs-meta prompt_">></span></code></pre> <p>To fully customize the output of a <a href="#class-replserver"><code>repl.REPLServer</code></a> instance pass in a new function for the <code>writer</code> option on construction. The following example, for instance, simply converts any input text to upper case:</p> <pre><code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:repl'</span>); <span class="hljs-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>({ <span class="hljs-attr">prompt</span>: <span class="hljs-string">'> '</span>, <span class="hljs-attr">eval</span>: myEval, <span class="hljs-attr">writer</span>: myWriter }); <span class="hljs-keyword">function</span> <span class="hljs-title function_">myEval</span>(<span class="hljs-params">cmd, context, filename, callback</span>) { <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, cmd); } <span class="hljs-keyword">function</span> <span class="hljs-title function_">myWriter</span>(<span class="hljs-params">output</span>) { <span class="hljs-keyword">return</span> output.<span class="hljs-title function_">toUpperCase</span>(); }</code></pre> </section><section><h3>Class: <code>REPLServer</code><span><a class="mark" href="#class-replserver" id="class-replserver">#</a></span><a aria-hidden="true" class="legacy" id="repl_class_replserver"></a></h3> <div class="api_metadata"> <span>Added in: v0.1.91</span> </div> <ul> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> See <a href="#replstartoptions"><code>repl.start()</code></a></li> <li>Extends: <a href="readline.html#class-interface" class="type"><readline.Interface></a></li> </ul> <p>Instances of <code>repl.REPLServer</code> are created using the <a href="#replstartoptions"><code>repl.start()</code></a> method or directly using the JavaScript <code>new</code> keyword.</p> <pre><code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:repl'</span>); <span class="hljs-keyword">const</span> options = { <span class="hljs-attr">useColors</span>: <span class="hljs-literal">true</span> }; <span class="hljs-keyword">const</span> firstInstance = repl.<span class="hljs-title function_">start</span>(options); <span class="hljs-keyword">const</span> secondInstance = <span class="hljs-keyword">new</span> repl.<span class="hljs-title class_">REPLServer</span>(options);</code></pre> <h4>Event: <code>'exit'</code><span><a class="mark" href="#event-exit" id="event-exit">#</a></span><a aria-hidden="true" class="legacy" id="repl_event_exit"></a></h4> <div class="api_metadata"> <span>Added in: v0.7.7</span> </div> <p>The <code>'exit'</code> event is emitted when the REPL is exited either by receiving the <code>.exit</code> command as input, the user pressing <kbd>Ctrl</kbd>+<kbd>C</kbd> twice to signal <code>SIGINT</code>, or by pressing <kbd>Ctrl</kbd>+<kbd>D</kbd> to signal <code>'end'</code> on the input stream. The listener callback is invoked without any arguments.</p> <pre><code class="language-js">replServer.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">() =></span> { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Received "exit" event from repl!'</span>); process.<span class="hljs-title function_">exit</span>(); });</code></pre> <h4>Event: <code>'reset'</code><span><a class="mark" href="#event-reset" id="event-reset">#</a></span><a aria-hidden="true" class="legacy" id="repl_event_reset"></a></h4> <div class="api_metadata"> <span>Added in: v0.11.0</span> </div> <p>The <code>'reset'</code> event is emitted when the REPL's context is reset. This occurs whenever the <code>.clear</code> command is received as input <em>unless</em> the REPL is using the default evaluator and the <code>repl.REPLServer</code> instance was created with the <code>useGlobal</code> option set to <code>true</code>. The listener callback will be called with a reference to the <code>context</code> object as the only argument.</p> <p>This can be used primarily to re-initialize REPL context to some pre-defined state:</p> <pre><code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:repl'</span>); <span class="hljs-keyword">function</span> <span class="hljs-title function_">initializeContext</span>(<span class="hljs-params">context</span>) { context.<span class="hljs-property">m</span> = <span class="hljs-string">'test'</span>; } <span class="hljs-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>({ <span class="hljs-attr">prompt</span>: <span class="hljs-string">'> '</span> }); <span class="hljs-title function_">initializeContext</span>(r.<span class="hljs-property">context</span>); r.<span class="hljs-title function_">on</span>(<span class="hljs-string">'reset'</span>, initializeContext);</code></pre> <p>When this code is executed, the global <code>'m'</code> variable can be modified but then reset to its initial value using the <code>.clear</code> command:</p> <pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">./node example.js</span> <span class="hljs-meta prompt_">> </span><span class="language-bash">m</span> 'test' <span class="hljs-meta prompt_">> </span><span class="language-bash">m = 1</span> 1 <span class="hljs-meta prompt_">> </span><span class="language-bash">m</span> 1 <span class="hljs-meta prompt_">> </span><span class="language-bash">.clear</span> Clearing context... <span class="hljs-meta prompt_">> </span><span class="language-bash">m</span> 'test' <span class="hljs-meta prompt_">></span></code></pre> <h4><code>replServer.defineCommand(keyword, cmd)</code><span><a class="mark" href="#replserverdefinecommandkeyword-cmd" id="replserverdefinecommandkeyword-cmd">#</a></span><a aria-hidden="true" class="legacy" id="repl_replserver_definecommand_keyword_cmd"></a></h4> <div class="api_metadata"> <span>Added in: v0.3.0</span> </div> <ul> <li><code>keyword</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The command keyword (<em>without</em> a leading <code>.</code> character).</li> <li><code>cmd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The function to invoke when the command is processed.</li> </ul> <p>The <code>replServer.defineCommand()</code> method is used to add new <code>.</code>-prefixed commands to the REPL instance. Such commands are invoked by typing a <code>.</code> followed by the <code>keyword</code>. The <code>cmd</code> is either a <code>Function</code> or an <code>Object</code> with the following properties:</p> <ul> <li><code>help</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Help text to be displayed when <code>.help</code> is entered (Optional).</li> <li><code>action</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The function to execute, optionally accepting a single string argument.</li> </ul> <p>The following example shows two new commands added to the REPL instance:</p> <pre><code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:repl'</span>); <span class="hljs-keyword">const</span> replServer = repl.<span class="hljs-title function_">start</span>({ <span class="hljs-attr">prompt</span>: <span class="hljs-string">'> '</span> }); replServer.<span class="hljs-title function_">defineCommand</span>(<span class="hljs-string">'sayhello'</span>, { <span class="hljs-attr">help</span>: <span class="hljs-string">'Say hello'</span>, <span class="hljs-title function_">action</span>(<span class="hljs-params">name</span>) { <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">clearBufferedCommand</span>(); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">`Hello, <span class="hljs-subst">${name}</span>!`</span>); <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">displayPrompt</span>(); } }); replServer.<span class="hljs-title function_">defineCommand</span>(<span class="hljs-string">'saybye'</span>, <span class="hljs-keyword">function</span> <span class="hljs-title function_">saybye</span>(<span class="hljs-params"></span>) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Goodbye!'</span>); <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">close</span>(); });</code></pre> <p>The new commands can then be used from within the REPL instance:</p> <pre><code class="language-console"><span class="hljs-meta prompt_">> </span><span class="language-bash">.sayhello Node.js User</span> Hello, Node.js User! <span class="hljs-meta prompt_">> </span><span class="language-bash">.saybye</span> Goodbye!</code></pre> <h4><code>replServer.displayPrompt([preserveCursor])</code><span><a class="mark" href="#replserverdisplaypromptpreservecursor" id="replserverdisplaypromptpreservecursor">#</a></span><a aria-hidden="true" class="legacy" id="repl_replserver_displayprompt_preservecursor"></a></h4> <div class="api_metadata"> <span>Added in: v0.1.91</span> </div> <ul> <li><code>preserveCursor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>The <code>replServer.displayPrompt()</code> method readies the REPL instance for input from the user, printing the configured <code>prompt</code> to a new line in the <code>output</code> and resuming the <code>input</code> to accept new input.</p> <p>When multi-line input is being entered, an ellipsis is printed rather than the 'prompt'.</p> <p>When <code>preserveCursor</code> is <code>true</code>, the cursor placement will not be reset to <code>0</code>.</p> <p>The <code>replServer.displayPrompt</code> method is primarily intended to be called from within the action function for commands registered using the <code>replServer.defineCommand()</code> method.</p> <h4><code>replServer.clearBufferedCommand()</code><span><a class="mark" href="#replserverclearbufferedcommand" id="replserverclearbufferedcommand">#</a></span><a aria-hidden="true" class="legacy" id="repl_replserver_clearbufferedcommand"></a></h4> <div class="api_metadata"> <span>Added in: v9.0.0</span> </div> <p>The <code>replServer.clearBufferedCommand()</code> method clears any command that has been buffered but not yet executed. This method is primarily intended to be called from within the action function for commands registered using the <code>replServer.defineCommand()</code> method.</p> <h4><code>replServer.parseREPLKeyword(keyword[, rest])</code><span><a class="mark" href="#replserverparsereplkeywordkeyword-rest" id="replserverparsereplkeywordkeyword-rest">#</a></span><a aria-hidden="true" class="legacy" id="repl_replserver_parsereplkeyword_keyword_rest"></a></h4> <div class="api_metadata"> <span>Added in: v0.8.9</span><span>Deprecated since: v9.0.0</span> </div> <p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated.</div><p></p> <ul> <li><code>keyword</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> the potential keyword to parse and execute</li> <li><code>rest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> any parameters to the keyword command</li> <li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li> </ul> <p>An internal method used to parse and execute <code>REPLServer</code> keywords. Returns <code>true</code> if <code>keyword</code> is a valid keyword, otherwise <code>false</code>.</p> <h4><code>replServer.setupHistory(historyPath, callback)</code><span><a class="mark" href="#replserversetuphistoryhistorypath-callback" id="replserversetuphistoryhistorypath-callback">#</a></span><a aria-hidden="true" class="legacy" id="repl_replserver_setuphistory_historypath_callback"></a></h4> <div class="api_metadata"> <span>Added in: v11.10.0</span> </div> <ul> <li><code>historyPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> the path to the history file</li> <li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> called when history writes are ready or upon error <ul> <li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type"><Error></a></li> <li><code>repl</code> <a href="repl.html#class-replserver" class="type"><repl.REPLServer></a></li> </ul> </li> </ul> <p>Initializes a history log file for the REPL instance. When executing the Node.js binary and using the command-line REPL, a history file is initialized by default. However, this is not the case when creating a REPL programmatically. Use this method to initialize a history log file when working with REPL instances programmatically.</p> </section><section><h3><code>repl.builtinModules</code><span><a class="mark" href="#replbuiltinmodules" id="replbuiltinmodules">#</a></span><a aria-hidden="true" class="legacy" id="repl_repl_builtinmodules"></a></h3> <div class="api_metadata"> <span>Added in: v14.5.0</span> </div> <ul> <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string[]></a></li> </ul> <p>A list of the names of all Node.js modules, e.g., <code>'http'</code>.</p> </section><section><h3><code>repl.start([options])</code><span><a class="mark" href="#replstartoptions" id="replstartoptions">#</a></span><a aria-hidden="true" class="legacy" id="repl_repl_start_options"></a></h3> <div class="api_metadata"> <details class="changelog"><summary>History</summary> <table> <tbody><tr><th>Version</th><th>Changes</th></tr> <tr><td>v13.4.0, v12.17.0</td> <td><p>The <code>preview</code> option is now available.</p></td></tr> <tr><td>v12.0.0</td> <td><p>The <code>terminal</code> option now follows the default description in all cases and <code>useColors</code> checks <code>hasColors()</code> if available.</p></td></tr> <tr><td>v10.0.0</td> <td><p>The <code>REPL_MAGIC_MODE</code> <code>replMode</code> was removed.</p></td></tr> <tr><td>v6.3.0</td> <td><p>The <code>breakEvalOnSigint</code> option is supported now.</p></td></tr> <tr><td>v5.8.0</td> <td><p>The <code>options</code> parameter is optional now.</p></td></tr> <tr><td>v0.1.91</td> <td><p><span>Added in: v0.1.91</span></p></td></tr> </tbody></table> </details> </div> <ul> <li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> <ul> <li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The input prompt to display. <strong>Default:</strong> <code>'> '</code> (with a trailing space).</li> <li><code>input</code> <a href="stream.html#class-streamreadable" class="type"><stream.Readable></a> The <code>Readable</code> stream from which REPL input will be read. <strong>Default:</strong> <code>process.stdin</code>.</li> <li><code>output</code> <a href="stream.html#class-streamwritable" class="type"><stream.Writable></a> The <code>Writable</code> stream to which REPL output will be written. <strong>Default:</strong> <code>process.stdout</code>.</li> <li><code>terminal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, specifies that the <code>output</code> should be treated as a TTY terminal. <strong>Default:</strong> checking the value of the <code>isTTY</code> property on the <code>output</code> stream upon instantiation.</li> <li><code>eval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The function to be used when evaluating each given line of input. <strong>Default:</strong> an async wrapper for the JavaScript <code>eval()</code> function. An <code>eval</code> function can error with <code>repl.Recoverable</code> to indicate the input was incomplete and prompt for additional lines.</li> <li><code>useColors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, specifies that the default <code>writer</code> function should include ANSI color styling to REPL output. If a custom <code>writer</code> function is provided then this has no effect. <strong>Default:</strong> checking color support on the <code>output</code> stream if the REPL instance's <code>terminal</code> value is <code>true</code>.</li> <li><code>useGlobal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, specifies that the default evaluation function will use the JavaScript <code>global</code> as the context as opposed to creating a new separate context for the REPL instance. The node CLI REPL sets this value to <code>true</code>. <strong>Default:</strong> <code>false</code>.</li> <li><code>ignoreUndefined</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, specifies that the default writer will not output the return value of a command if it evaluates to <code>undefined</code>. <strong>Default:</strong> <code>false</code>.</li> <li><code>writer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The function to invoke to format the output of each command before writing to <code>output</code>. <strong>Default:</strong> <a href="util.html#utilinspectobject-options"><code>util.inspect()</code></a>.</li> <li><code>completer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> An optional function used for custom Tab auto completion. See <a href="readline.html#use-of-the-completer-function"><code>readline.InterfaceCompleter</code></a> for an example.</li> <li><code>replMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> A flag that specifies whether the default evaluator executes all JavaScript commands in strict mode or default (sloppy) mode. Acceptable values are: <ul> <li><code>repl.REPL_MODE_SLOPPY</code> to evaluate expressions in sloppy mode.</li> <li><code>repl.REPL_MODE_STRICT</code> to evaluate expressions in strict mode. This is equivalent to prefacing every repl statement with <code>'use strict'</code>.</li> </ul> </li> <li><code>breakEvalOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Stop evaluating the current piece of code when <code>SIGINT</code> is received, such as when <kbd>Ctrl</kbd>+<kbd>C</kbd> is pressed. This cannot be used together with a custom <code>eval</code> function. <strong>Default:</strong> <code>false</code>.</li> <li><code>preview</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Defines if the repl prints autocomplete and output previews or not. <strong>Default:</strong> <code>true</code> with the default eval function and <code>false</code> in case a custom eval function is used. If <code>terminal</code> is falsy, then there are no previews and the value of <code>preview</code> has no effect.</li> </ul> </li> <li>Returns: <a href="repl.html#class-replserver" class="type"><repl.REPLServer></a></li> </ul> <p>The <code>repl.start()</code> method creates and starts a <a href="#class-replserver"><code>repl.REPLServer</code></a> instance.</p> <p>If <code>options</code> is a string, then it specifies the input prompt:</p> <pre><code class="language-js"><span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:repl'</span>); <span class="hljs-comment">// a Unix style prompt</span> repl.<span class="hljs-title function_">start</span>(<span class="hljs-string">'$ '</span>);</code></pre> </section><section><h3>The Node.js REPL<span><a class="mark" href="#the-nodejs-repl" id="the-nodejs-repl">#</a></span><a aria-hidden="true" class="legacy" id="repl_the_node_js_repl"></a></h3> <p>Node.js itself uses the <code>node:repl</code> module to provide its own interactive interface for executing JavaScript. This can be used by executing the Node.js binary without passing any arguments (or by passing the <code>-i</code> argument):</p> <pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node</span> <span class="hljs-meta prompt_">> </span><span class="language-bash">const a = [1, 2, 3];</span> undefined <span class="hljs-meta prompt_">> </span><span class="language-bash">a</span> [ 1, 2, 3 ] <span class="hljs-meta prompt_">> </span><span class="language-bash">a.forEach((v) => {</span> ... console.log(v); ... }); 1 2 3</code></pre> <h4>Environment variable options<span><a class="mark" href="#environment-variable-options" id="environment-variable-options">#</a></span><a aria-hidden="true" class="legacy" id="repl_environment_variable_options"></a></h4> <p>Various behaviors of the Node.js REPL can be customized using the following environment variables:</p> <ul> <li><code>NODE_REPL_HISTORY</code>: When a valid path is given, persistent REPL history will be saved to the specified file rather than <code>.node_repl_history</code> in the user's home directory. Setting this value to <code>''</code> (an empty string) will disable persistent REPL history. Whitespace will be trimmed from the value. On Windows platforms environment variables with empty values are invalid so set this variable to one or more spaces to disable persistent REPL history.</li> <li><code>NODE_REPL_HISTORY_SIZE</code>: Controls how many lines of history will be persisted if history is available. Must be a positive number. <strong>Default:</strong> <code>1000</code>.</li> <li><code>NODE_REPL_MODE</code>: May be either <code>'sloppy'</code> or <code>'strict'</code>. <strong>Default:</strong> <code>'sloppy'</code>, which will allow non-strict mode code to be run.</li> </ul> <h4>Persistent history<span><a class="mark" href="#persistent-history" id="persistent-history">#</a></span><a aria-hidden="true" class="legacy" id="repl_persistent_history"></a></h4> <p>By default, the Node.js REPL will persist history between <code>node</code> REPL sessions by saving inputs to a <code>.node_repl_history</code> file located in the user's home directory. This can be disabled by setting the environment variable <code>NODE_REPL_HISTORY=''</code>.</p> <h4>Using the Node.js REPL with advanced line-editors<span><a class="mark" href="#using-the-nodejs-repl-with-advanced-line-editors" id="using-the-nodejs-repl-with-advanced-line-editors">#</a></span><a aria-hidden="true" class="legacy" id="repl_using_the_node_js_repl_with_advanced_line_editors"></a></h4> <p>For advanced line-editors, start Node.js with the environment variable <code>NODE_NO_READLINE=1</code>. This will start the main and debugger REPL in canonical terminal settings, which will allow use with <code>rlwrap</code>.</p> <p>For example, the following can be added to a <code>.bashrc</code> file:</p> <pre><code class="language-text">alias node="env NODE_NO_READLINE=1 rlwrap node"</code></pre> <h4>Starting multiple REPL instances against a single running instance<span><a class="mark" href="#starting-multiple-repl-instances-against-a-single-running-instance" id="starting-multiple-repl-instances-against-a-single-running-instance">#</a></span><a aria-hidden="true" class="legacy" id="repl_starting_multiple_repl_instances_against_a_single_running_instance"></a></h4> <p>It is possible to create and run multiple REPL instances against a single running instance of Node.js that share a single <code>global</code> object but have separate I/O interfaces.</p> <p>The following example, for instance, provides separate REPLs on <code>stdin</code>, a Unix socket, and a TCP socket:</p> <pre><code class="language-js"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:net'</span>); <span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:repl'</span>); <span class="hljs-keyword">let</span> connections = <span class="hljs-number">0</span>; repl.<span class="hljs-title function_">start</span>({ <span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via stdin> '</span>, <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>, <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span> }); net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> { connections += <span class="hljs-number">1</span>; repl.<span class="hljs-title function_">start</span>({ <span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via Unix socket> '</span>, <span class="hljs-attr">input</span>: socket, <span class="hljs-attr">output</span>: socket }).<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">() =></span> { socket.<span class="hljs-title function_">end</span>(); }); }).<span class="hljs-title function_">listen</span>(<span class="hljs-string">'/tmp/node-repl-sock'</span>); net.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> { connections += <span class="hljs-number">1</span>; repl.<span class="hljs-title function_">start</span>({ <span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via TCP socket> '</span>, <span class="hljs-attr">input</span>: socket, <span class="hljs-attr">output</span>: socket }).<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">() =></span> { socket.<span class="hljs-title function_">end</span>(); }); }).<span class="hljs-title function_">listen</span>(<span class="hljs-number">5001</span>);</code></pre> <p>Running this application from the command line will start a REPL on stdin. Other REPL clients may connect through the Unix socket or TCP socket. <code>telnet</code>, for instance, is useful for connecting to TCP sockets, while <code>socat</code> can be used to connect to both Unix and TCP sockets.</p> <p>By starting a REPL from a Unix socket-based server instead of stdin, it is possible to connect to a long-running Node.js process without restarting it.</p> <p>For an example of running a "full-featured" (<code>terminal</code>) REPL over a <code>net.Server</code> and <code>net.Socket</code> instance, see: <a href="https://gist.github.com/TooTallNate/2209310">https://gist.github.com/TooTallNate/2209310</a>.</p> <p>For an example of running a REPL instance over <a href="https://curl.haxx.se/docs/manpage.html"><code>curl(1)</code></a>, see: <a href="https://gist.github.com/TooTallNate/2053342">https://gist.github.com/TooTallNate/2053342</a>.</p></section> <!-- API END --> </div> </div> </div> </body> </html>
.
Edit
..
Edit
addons.html
Edit
addons.json
Edit
addons.md
Edit
all.html
Edit
all.json
Edit
assert.html
Edit
assert.json
Edit
assert.md
Edit
assets
Edit
async_context.html
Edit
async_context.json
Edit
async_context.md
Edit
async_hooks.html
Edit
async_hooks.json
Edit
async_hooks.md
Edit
buffer.html
Edit
buffer.json
Edit
buffer.md
Edit
child_process.html
Edit
child_process.json
Edit
child_process.md
Edit
cli.html
Edit
cli.json
Edit
cli.md
Edit
cluster.html
Edit
cluster.json
Edit
cluster.md
Edit
console.html
Edit
console.json
Edit
console.md
Edit
corepack.html
Edit
corepack.json
Edit
corepack.md
Edit
crypto.html
Edit
crypto.json
Edit
crypto.md
Edit
debugger.html
Edit
debugger.json
Edit
debugger.md
Edit
deprecations.html
Edit
deprecations.json
Edit
deprecations.md
Edit
dgram.html
Edit
dgram.json
Edit
dgram.md
Edit
diagnostics_channel.html
Edit
diagnostics_channel.json
Edit
diagnostics_channel.md
Edit
dns.html
Edit
dns.json
Edit
dns.md
Edit
documentation.html
Edit
documentation.json
Edit
documentation.md
Edit
domain.html
Edit
domain.json
Edit
domain.md
Edit
embedding.html
Edit
embedding.json
Edit
embedding.md
Edit
errors.html
Edit
errors.json
Edit
errors.md
Edit
esm.html
Edit
esm.json
Edit
esm.md
Edit
events.html
Edit
events.json
Edit
events.md
Edit
fs.html
Edit
fs.json
Edit
fs.md
Edit
globals.html
Edit
globals.json
Edit
globals.md
Edit
http.html
Edit
http.json
Edit
http.md
Edit
http2.html
Edit
http2.json
Edit
http2.md
Edit
https.html
Edit
https.json
Edit
https.md
Edit
index.html
Edit
index.json
Edit
index.md
Edit
inspector.html
Edit
inspector.json
Edit
inspector.md
Edit
intl.html
Edit
intl.json
Edit
intl.md
Edit
module.html
Edit
module.json
Edit
module.md
Edit
modules.html
Edit
modules.json
Edit
modules.md
Edit
n-api.html
Edit
n-api.json
Edit
n-api.md
Edit
net.html
Edit
net.json
Edit
net.md
Edit
os.html
Edit
os.json
Edit
os.md
Edit
packages.html
Edit
packages.json
Edit
packages.md
Edit
path.html
Edit
path.json
Edit
path.md
Edit
perf_hooks.html
Edit
perf_hooks.json
Edit
perf_hooks.md
Edit
permissions.html
Edit
permissions.json
Edit
permissions.md
Edit
policy.html
Edit
policy.json
Edit
policy.md
Edit
process.html
Edit
process.json
Edit
process.md
Edit
punycode.html
Edit
punycode.json
Edit
punycode.md
Edit
querystring.html
Edit
querystring.json
Edit
querystring.md
Edit
readline.html
Edit
readline.json
Edit
readline.md
Edit
repl.html
Edit
repl.json
Edit
repl.md
Edit
report.html
Edit
report.json
Edit
report.md
Edit
stream.html
Edit
stream.json
Edit
stream.md
Edit
string_decoder.html
Edit
string_decoder.json
Edit
string_decoder.md
Edit
synopsis.html
Edit
synopsis.json
Edit
synopsis.md
Edit
test.html
Edit
test.json
Edit
test.md
Edit
timers.html
Edit
timers.json
Edit
timers.md
Edit
tls.html
Edit
tls.json
Edit
tls.md
Edit
tracing.html
Edit
tracing.json
Edit
tracing.md
Edit
tty.html
Edit
tty.json
Edit
tty.md
Edit
url.html
Edit
url.json
Edit
url.md
Edit
util.html
Edit
util.json
Edit
util.md
Edit
v8.html
Edit
v8.json
Edit
v8.md
Edit
vm.html
Edit
vm.json
Edit
vm.md
Edit
wasi.html
Edit
wasi.json
Edit
wasi.md
Edit
webcrypto.html
Edit
webcrypto.json
Edit
webcrypto.md
Edit
webstreams.html
Edit
webstreams.json
Edit
webstreams.md
Edit
worker_threads.html
Edit
worker_threads.json
Edit
worker_threads.md
Edit
zlib.html
Edit
zlib.json
Edit
zlib.md
Edit