11cb0ef41Sopenharmony_ci<!DOCTYPE html>
21cb0ef41Sopenharmony_ci<html lang="en">
31cb0ef41Sopenharmony_ci<head>
41cb0ef41Sopenharmony_ci  <meta charset="utf-8">
51cb0ef41Sopenharmony_ci  <meta name="viewport" content="width=device-width">
61cb0ef41Sopenharmony_ci  <meta name="nodejs.org:node-version" content="v18.20.1">
71cb0ef41Sopenharmony_ci  <title>REPL | Node.js v18.20.1 Documentation</title>
81cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
91cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="assets/style.css">
101cb0ef41Sopenharmony_ci  <link rel="stylesheet" href="assets/hljs.css">
111cb0ef41Sopenharmony_ci  <link rel="canonical" href="https://nodejs.org/api/repl.html">
121cb0ef41Sopenharmony_ci  <script async defer src="assets/api.js" type="text/javascript"></script>
131cb0ef41Sopenharmony_ci  
141cb0ef41Sopenharmony_ci</head>
151cb0ef41Sopenharmony_ci<body class="alt apidoc" id="api-section-repl">
161cb0ef41Sopenharmony_ci  <div id="content" class="clearfix">
171cb0ef41Sopenharmony_ci    <div id="column2" class="interior">
181cb0ef41Sopenharmony_ci      <div id="intro" class="interior">
191cb0ef41Sopenharmony_ci        <a href="/" title="Go back to the home page">
201cb0ef41Sopenharmony_ci          Node.js
211cb0ef41Sopenharmony_ci        </a>
221cb0ef41Sopenharmony_ci      </div>
231cb0ef41Sopenharmony_ci      <ul>
241cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
251cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
261cb0ef41Sopenharmony_ci</ul>
271cb0ef41Sopenharmony_ci<hr class="line">
281cb0ef41Sopenharmony_ci<ul>
291cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
301cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
311cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
321cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
331cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
341cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
351cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
361cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
371cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
381cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
391cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li>
401cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
411cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
421cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
431cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
441cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
451cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li>
461cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li>
471cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li>
481cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li>
491cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li>
501cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li>
511cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li>
521cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
531cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li>
541cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
551cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
561cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
571cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
581cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
591cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
601cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li>
611cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li>
621cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li>
631cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
641cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
651cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li>
661cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
671cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
681cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li>
691cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl active">REPL</a></li>
701cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li>
711cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
721cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li>
731cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
741cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li>
751cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li>
761cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
771cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
781cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li>
791cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
801cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li>
811cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li>
821cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li>
831cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li>
841cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
851cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
861cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
871cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
881cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
891cb0ef41Sopenharmony_ci</ul>
901cb0ef41Sopenharmony_ci<hr class="line">
911cb0ef41Sopenharmony_ci<ul>
921cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
931cb0ef41Sopenharmony_ci</ul>
941cb0ef41Sopenharmony_ci    </div>
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci    <div id="column1" data-id="repl" class="interior">
971cb0ef41Sopenharmony_ci      <header class="header">
981cb0ef41Sopenharmony_ci        <div class="header-container">
991cb0ef41Sopenharmony_ci          <h1>Node.js v18.20.1 documentation</h1>
1001cb0ef41Sopenharmony_ci          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
1011cb0ef41Sopenharmony_ci            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
1021cb0ef41Sopenharmony_ci              <path fill="none" d="M0 0h24v24H0z" />
1031cb0ef41Sopenharmony_ci              <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"/>
1041cb0ef41Sopenharmony_ci              <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"/>
1051cb0ef41Sopenharmony_ci            </svg>
1061cb0ef41Sopenharmony_ci            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
1071cb0ef41Sopenharmony_ci              <path d="M0 0h24v24H0z" fill="none" />
1081cb0ef41Sopenharmony_ci              <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"/>
1091cb0ef41Sopenharmony_ci            </svg>
1101cb0ef41Sopenharmony_ci          </button>
1111cb0ef41Sopenharmony_ci        </div>
1121cb0ef41Sopenharmony_ci        <div id="gtoc">
1131cb0ef41Sopenharmony_ci          <ul>
1141cb0ef41Sopenharmony_ci            <li class="pinned-header">Node.js v18.20.1</li>
1151cb0ef41Sopenharmony_ci            
1161cb0ef41Sopenharmony_ci    <li class="picker-header">
1171cb0ef41Sopenharmony_ci      <a href="#">
1181cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
1191cb0ef41Sopenharmony_ci        Table of contents
1201cb0ef41Sopenharmony_ci      </a>
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci      <div class="picker"><div class="toc"><ul>
1231cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#repl">REPL</a></span>
1241cb0ef41Sopenharmony_ci<ul>
1251cb0ef41Sopenharmony_ci<li><a href="#design-and-features">Design and features</a>
1261cb0ef41Sopenharmony_ci<ul>
1271cb0ef41Sopenharmony_ci<li><a href="#commands-and-special-keys">Commands and special keys</a></li>
1281cb0ef41Sopenharmony_ci<li><a href="#default-evaluation">Default evaluation</a>
1291cb0ef41Sopenharmony_ci<ul>
1301cb0ef41Sopenharmony_ci<li><a href="#javascript-expressions">JavaScript expressions</a></li>
1311cb0ef41Sopenharmony_ci<li><a href="#global-and-local-scope">Global and local scope</a></li>
1321cb0ef41Sopenharmony_ci<li><a href="#accessing-core-nodejs-modules">Accessing core Node.js modules</a></li>
1331cb0ef41Sopenharmony_ci<li><a href="#global-uncaught-exceptions">Global uncaught exceptions</a></li>
1341cb0ef41Sopenharmony_ci<li><a href="#assignment-of-the-_-underscore-variable">Assignment of the <code>_</code> (underscore) variable</a></li>
1351cb0ef41Sopenharmony_ci<li><a href="#await-keyword"><code>await</code> keyword</a></li>
1361cb0ef41Sopenharmony_ci</ul>
1371cb0ef41Sopenharmony_ci</li>
1381cb0ef41Sopenharmony_ci<li><a href="#reverse-i-search">Reverse-i-search</a></li>
1391cb0ef41Sopenharmony_ci<li><a href="#custom-evaluation-functions">Custom evaluation functions</a>
1401cb0ef41Sopenharmony_ci<ul>
1411cb0ef41Sopenharmony_ci<li><a href="#recoverable-errors">Recoverable errors</a></li>
1421cb0ef41Sopenharmony_ci</ul>
1431cb0ef41Sopenharmony_ci</li>
1441cb0ef41Sopenharmony_ci<li><a href="#customizing-repl-output">Customizing REPL output</a></li>
1451cb0ef41Sopenharmony_ci</ul>
1461cb0ef41Sopenharmony_ci</li>
1471cb0ef41Sopenharmony_ci<li><a href="#class-replserver">Class: <code>REPLServer</code></a>
1481cb0ef41Sopenharmony_ci<ul>
1491cb0ef41Sopenharmony_ci<li><a href="#event-exit">Event: <code>'exit'</code></a></li>
1501cb0ef41Sopenharmony_ci<li><a href="#event-reset">Event: <code>'reset'</code></a></li>
1511cb0ef41Sopenharmony_ci<li><a href="#replserverdefinecommandkeyword-cmd"><code>replServer.defineCommand(keyword, cmd)</code></a></li>
1521cb0ef41Sopenharmony_ci<li><a href="#replserverdisplaypromptpreservecursor"><code>replServer.displayPrompt([preserveCursor])</code></a></li>
1531cb0ef41Sopenharmony_ci<li><a href="#replserverclearbufferedcommand"><code>replServer.clearBufferedCommand()</code></a></li>
1541cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#replserverparsereplkeywordkeyword-rest"><code>replServer.parseREPLKeyword(keyword[, rest])</code></a></span></li>
1551cb0ef41Sopenharmony_ci<li><a href="#replserversetuphistoryhistorypath-callback"><code>replServer.setupHistory(historyPath, callback)</code></a></li>
1561cb0ef41Sopenharmony_ci</ul>
1571cb0ef41Sopenharmony_ci</li>
1581cb0ef41Sopenharmony_ci<li><a href="#replbuiltinmodules"><code>repl.builtinModules</code></a></li>
1591cb0ef41Sopenharmony_ci<li><a href="#replstartoptions"><code>repl.start([options])</code></a></li>
1601cb0ef41Sopenharmony_ci<li><a href="#the-nodejs-repl">The Node.js REPL</a>
1611cb0ef41Sopenharmony_ci<ul>
1621cb0ef41Sopenharmony_ci<li><a href="#environment-variable-options">Environment variable options</a></li>
1631cb0ef41Sopenharmony_ci<li><a href="#persistent-history">Persistent history</a></li>
1641cb0ef41Sopenharmony_ci<li><a href="#using-the-nodejs-repl-with-advanced-line-editors">Using the Node.js REPL with advanced line-editors</a></li>
1651cb0ef41Sopenharmony_ci<li><a href="#starting-multiple-repl-instances-against-a-single-running-instance">Starting multiple REPL instances against a single running instance</a></li>
1661cb0ef41Sopenharmony_ci</ul>
1671cb0ef41Sopenharmony_ci</li>
1681cb0ef41Sopenharmony_ci</ul>
1691cb0ef41Sopenharmony_ci</li>
1701cb0ef41Sopenharmony_ci</ul></div></div>
1711cb0ef41Sopenharmony_ci    </li>
1721cb0ef41Sopenharmony_ci  
1731cb0ef41Sopenharmony_ci            
1741cb0ef41Sopenharmony_ci    <li class="picker-header">
1751cb0ef41Sopenharmony_ci      <a href="#">
1761cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
1771cb0ef41Sopenharmony_ci        Index
1781cb0ef41Sopenharmony_ci      </a>
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci      <div class="picker"><ul>
1811cb0ef41Sopenharmony_ci<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
1821cb0ef41Sopenharmony_ci<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci      <li>
1851cb0ef41Sopenharmony_ci        <a href="index.html">Index</a>
1861cb0ef41Sopenharmony_ci      </li>
1871cb0ef41Sopenharmony_ci    </ul>
1881cb0ef41Sopenharmony_ci  
1891cb0ef41Sopenharmony_ci<hr class="line">
1901cb0ef41Sopenharmony_ci<ul>
1911cb0ef41Sopenharmony_ci<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
1921cb0ef41Sopenharmony_ci<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
1931cb0ef41Sopenharmony_ci<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
1941cb0ef41Sopenharmony_ci<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
1951cb0ef41Sopenharmony_ci<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
1961cb0ef41Sopenharmony_ci<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
1971cb0ef41Sopenharmony_ci<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
1981cb0ef41Sopenharmony_ci<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
1991cb0ef41Sopenharmony_ci<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
2001cb0ef41Sopenharmony_ci<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
2011cb0ef41Sopenharmony_ci<li><a href="console.html" class="nav-console">Console</a></li>
2021cb0ef41Sopenharmony_ci<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
2031cb0ef41Sopenharmony_ci<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
2041cb0ef41Sopenharmony_ci<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
2051cb0ef41Sopenharmony_ci<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
2061cb0ef41Sopenharmony_ci<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
2071cb0ef41Sopenharmony_ci<li><a href="dns.html" class="nav-dns">DNS</a></li>
2081cb0ef41Sopenharmony_ci<li><a href="domain.html" class="nav-domain">Domain</a></li>
2091cb0ef41Sopenharmony_ci<li><a href="errors.html" class="nav-errors">Errors</a></li>
2101cb0ef41Sopenharmony_ci<li><a href="events.html" class="nav-events">Events</a></li>
2111cb0ef41Sopenharmony_ci<li><a href="fs.html" class="nav-fs">File system</a></li>
2121cb0ef41Sopenharmony_ci<li><a href="globals.html" class="nav-globals">Globals</a></li>
2131cb0ef41Sopenharmony_ci<li><a href="http.html" class="nav-http">HTTP</a></li>
2141cb0ef41Sopenharmony_ci<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
2151cb0ef41Sopenharmony_ci<li><a href="https.html" class="nav-https">HTTPS</a></li>
2161cb0ef41Sopenharmony_ci<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
2171cb0ef41Sopenharmony_ci<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
2181cb0ef41Sopenharmony_ci<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
2191cb0ef41Sopenharmony_ci<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
2201cb0ef41Sopenharmony_ci<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
2211cb0ef41Sopenharmony_ci<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
2221cb0ef41Sopenharmony_ci<li><a href="net.html" class="nav-net">Net</a></li>
2231cb0ef41Sopenharmony_ci<li><a href="os.html" class="nav-os">OS</a></li>
2241cb0ef41Sopenharmony_ci<li><a href="path.html" class="nav-path">Path</a></li>
2251cb0ef41Sopenharmony_ci<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
2261cb0ef41Sopenharmony_ci<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
2271cb0ef41Sopenharmony_ci<li><a href="process.html" class="nav-process">Process</a></li>
2281cb0ef41Sopenharmony_ci<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
2291cb0ef41Sopenharmony_ci<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
2301cb0ef41Sopenharmony_ci<li><a href="readline.html" class="nav-readline">Readline</a></li>
2311cb0ef41Sopenharmony_ci<li><a href="repl.html" class="nav-repl active">REPL</a></li>
2321cb0ef41Sopenharmony_ci<li><a href="report.html" class="nav-report">Report</a></li>
2331cb0ef41Sopenharmony_ci<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
2341cb0ef41Sopenharmony_ci<li><a href="stream.html" class="nav-stream">Stream</a></li>
2351cb0ef41Sopenharmony_ci<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
2361cb0ef41Sopenharmony_ci<li><a href="test.html" class="nav-test">Test runner</a></li>
2371cb0ef41Sopenharmony_ci<li><a href="timers.html" class="nav-timers">Timers</a></li>
2381cb0ef41Sopenharmony_ci<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
2391cb0ef41Sopenharmony_ci<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
2401cb0ef41Sopenharmony_ci<li><a href="tty.html" class="nav-tty">TTY</a></li>
2411cb0ef41Sopenharmony_ci<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
2421cb0ef41Sopenharmony_ci<li><a href="url.html" class="nav-url">URL</a></li>
2431cb0ef41Sopenharmony_ci<li><a href="util.html" class="nav-util">Utilities</a></li>
2441cb0ef41Sopenharmony_ci<li><a href="v8.html" class="nav-v8">V8</a></li>
2451cb0ef41Sopenharmony_ci<li><a href="vm.html" class="nav-vm">VM</a></li>
2461cb0ef41Sopenharmony_ci<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
2471cb0ef41Sopenharmony_ci<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
2481cb0ef41Sopenharmony_ci<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
2491cb0ef41Sopenharmony_ci<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
2501cb0ef41Sopenharmony_ci<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
2511cb0ef41Sopenharmony_ci</ul>
2521cb0ef41Sopenharmony_ci<hr class="line">
2531cb0ef41Sopenharmony_ci<ul>
2541cb0ef41Sopenharmony_ci<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
2551cb0ef41Sopenharmony_ci</ul></div>
2561cb0ef41Sopenharmony_ci    </li>
2571cb0ef41Sopenharmony_ci  
2581cb0ef41Sopenharmony_ci            
2591cb0ef41Sopenharmony_ci    <li class="picker-header">
2601cb0ef41Sopenharmony_ci      <a href="#">
2611cb0ef41Sopenharmony_ci        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
2621cb0ef41Sopenharmony_ci        Other versions
2631cb0ef41Sopenharmony_ci      </a>
2641cb0ef41Sopenharmony_ci      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/repl.html">21.x</a></li>
2651cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v20.x/api/repl.html">20.x <b>LTS</b></a></li>
2661cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v19.x/api/repl.html">19.x</a></li>
2671cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v18.x/api/repl.html">18.x <b>LTS</b></a></li>
2681cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v17.x/api/repl.html">17.x</a></li>
2691cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v16.x/api/repl.html">16.x</a></li>
2701cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v15.x/api/repl.html">15.x</a></li>
2711cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v14.x/api/repl.html">14.x</a></li>
2721cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v13.x/api/repl.html">13.x</a></li>
2731cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v12.x/api/repl.html">12.x</a></li>
2741cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v11.x/api/repl.html">11.x</a></li>
2751cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v10.x/api/repl.html">10.x</a></li>
2761cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v9.x/api/repl.html">9.x</a></li>
2771cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v8.x/api/repl.html">8.x</a></li>
2781cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v7.x/api/repl.html">7.x</a></li>
2791cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v6.x/api/repl.html">6.x</a></li>
2801cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v5.x/api/repl.html">5.x</a></li>
2811cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v4.x/api/repl.html">4.x</a></li>
2821cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/repl.html">0.12.x</a></li>
2831cb0ef41Sopenharmony_ci<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/repl.html">0.10.x</a></li></ol></div>
2841cb0ef41Sopenharmony_ci    </li>
2851cb0ef41Sopenharmony_ci  
2861cb0ef41Sopenharmony_ci            <li class="picker-header">
2871cb0ef41Sopenharmony_ci              <a href="#">
2881cb0ef41Sopenharmony_ci                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
2891cb0ef41Sopenharmony_ci                Options
2901cb0ef41Sopenharmony_ci              </a>
2911cb0ef41Sopenharmony_ci        
2921cb0ef41Sopenharmony_ci              <div class="picker">
2931cb0ef41Sopenharmony_ci                <ul>
2941cb0ef41Sopenharmony_ci                  <li>
2951cb0ef41Sopenharmony_ci                    <a href="all.html">View on single page</a>
2961cb0ef41Sopenharmony_ci                  </li>
2971cb0ef41Sopenharmony_ci                  <li>
2981cb0ef41Sopenharmony_ci                    <a href="repl.json">View as JSON</a>
2991cb0ef41Sopenharmony_ci                  </li>
3001cb0ef41Sopenharmony_ci                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/repl.md">Edit on GitHub</a></li>    
3011cb0ef41Sopenharmony_ci                </ul>
3021cb0ef41Sopenharmony_ci              </div>
3031cb0ef41Sopenharmony_ci            </li>
3041cb0ef41Sopenharmony_ci          </ul>
3051cb0ef41Sopenharmony_ci        </div>
3061cb0ef41Sopenharmony_ci        <hr>
3071cb0ef41Sopenharmony_ci      </header>
3081cb0ef41Sopenharmony_ci
3091cb0ef41Sopenharmony_ci      <details id="toc" open><summary>Table of contents</summary><ul>
3101cb0ef41Sopenharmony_ci<li><span class="stability_2"><a href="#repl">REPL</a></span>
3111cb0ef41Sopenharmony_ci<ul>
3121cb0ef41Sopenharmony_ci<li><a href="#design-and-features">Design and features</a>
3131cb0ef41Sopenharmony_ci<ul>
3141cb0ef41Sopenharmony_ci<li><a href="#commands-and-special-keys">Commands and special keys</a></li>
3151cb0ef41Sopenharmony_ci<li><a href="#default-evaluation">Default evaluation</a>
3161cb0ef41Sopenharmony_ci<ul>
3171cb0ef41Sopenharmony_ci<li><a href="#javascript-expressions">JavaScript expressions</a></li>
3181cb0ef41Sopenharmony_ci<li><a href="#global-and-local-scope">Global and local scope</a></li>
3191cb0ef41Sopenharmony_ci<li><a href="#accessing-core-nodejs-modules">Accessing core Node.js modules</a></li>
3201cb0ef41Sopenharmony_ci<li><a href="#global-uncaught-exceptions">Global uncaught exceptions</a></li>
3211cb0ef41Sopenharmony_ci<li><a href="#assignment-of-the-_-underscore-variable">Assignment of the <code>_</code> (underscore) variable</a></li>
3221cb0ef41Sopenharmony_ci<li><a href="#await-keyword"><code>await</code> keyword</a></li>
3231cb0ef41Sopenharmony_ci</ul>
3241cb0ef41Sopenharmony_ci</li>
3251cb0ef41Sopenharmony_ci<li><a href="#reverse-i-search">Reverse-i-search</a></li>
3261cb0ef41Sopenharmony_ci<li><a href="#custom-evaluation-functions">Custom evaluation functions</a>
3271cb0ef41Sopenharmony_ci<ul>
3281cb0ef41Sopenharmony_ci<li><a href="#recoverable-errors">Recoverable errors</a></li>
3291cb0ef41Sopenharmony_ci</ul>
3301cb0ef41Sopenharmony_ci</li>
3311cb0ef41Sopenharmony_ci<li><a href="#customizing-repl-output">Customizing REPL output</a></li>
3321cb0ef41Sopenharmony_ci</ul>
3331cb0ef41Sopenharmony_ci</li>
3341cb0ef41Sopenharmony_ci<li><a href="#class-replserver">Class: <code>REPLServer</code></a>
3351cb0ef41Sopenharmony_ci<ul>
3361cb0ef41Sopenharmony_ci<li><a href="#event-exit">Event: <code>'exit'</code></a></li>
3371cb0ef41Sopenharmony_ci<li><a href="#event-reset">Event: <code>'reset'</code></a></li>
3381cb0ef41Sopenharmony_ci<li><a href="#replserverdefinecommandkeyword-cmd"><code>replServer.defineCommand(keyword, cmd)</code></a></li>
3391cb0ef41Sopenharmony_ci<li><a href="#replserverdisplaypromptpreservecursor"><code>replServer.displayPrompt([preserveCursor])</code></a></li>
3401cb0ef41Sopenharmony_ci<li><a href="#replserverclearbufferedcommand"><code>replServer.clearBufferedCommand()</code></a></li>
3411cb0ef41Sopenharmony_ci<li><span class="stability_0"><a href="#replserverparsereplkeywordkeyword-rest"><code>replServer.parseREPLKeyword(keyword[, rest])</code></a></span></li>
3421cb0ef41Sopenharmony_ci<li><a href="#replserversetuphistoryhistorypath-callback"><code>replServer.setupHistory(historyPath, callback)</code></a></li>
3431cb0ef41Sopenharmony_ci</ul>
3441cb0ef41Sopenharmony_ci</li>
3451cb0ef41Sopenharmony_ci<li><a href="#replbuiltinmodules"><code>repl.builtinModules</code></a></li>
3461cb0ef41Sopenharmony_ci<li><a href="#replstartoptions"><code>repl.start([options])</code></a></li>
3471cb0ef41Sopenharmony_ci<li><a href="#the-nodejs-repl">The Node.js REPL</a>
3481cb0ef41Sopenharmony_ci<ul>
3491cb0ef41Sopenharmony_ci<li><a href="#environment-variable-options">Environment variable options</a></li>
3501cb0ef41Sopenharmony_ci<li><a href="#persistent-history">Persistent history</a></li>
3511cb0ef41Sopenharmony_ci<li><a href="#using-the-nodejs-repl-with-advanced-line-editors">Using the Node.js REPL with advanced line-editors</a></li>
3521cb0ef41Sopenharmony_ci<li><a href="#starting-multiple-repl-instances-against-a-single-running-instance">Starting multiple REPL instances against a single running instance</a></li>
3531cb0ef41Sopenharmony_ci</ul>
3541cb0ef41Sopenharmony_ci</li>
3551cb0ef41Sopenharmony_ci</ul>
3561cb0ef41Sopenharmony_ci</li>
3571cb0ef41Sopenharmony_ci</ul></details>
3581cb0ef41Sopenharmony_ci
3591cb0ef41Sopenharmony_ci      <div id="apicontent">
3601cb0ef41Sopenharmony_ci        <h2>REPL<span><a class="mark" href="#repl" id="repl">#</a></span><a aria-hidden="true" class="legacy" id="repl_repl"></a></h2>
3611cb0ef41Sopenharmony_ci
3621cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
3631cb0ef41Sopenharmony_ci<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/repl.js">lib/repl.js</a></p>
3641cb0ef41Sopenharmony_ci<p>The <code>node:repl</code> module provides a Read-Eval-Print-Loop (REPL) implementation
3651cb0ef41Sopenharmony_cithat is available both as a standalone program or includible in other
3661cb0ef41Sopenharmony_ciapplications. It can be accessed using:</p>
3671cb0ef41Sopenharmony_ci<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> <button class="copy-button">copy</button></pre>
3681cb0ef41Sopenharmony_ci<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>
3691cb0ef41Sopenharmony_ci<p>The <code>node:repl</code> module exports the <a href="#class-replserver"><code>repl.REPLServer</code></a> class. While running,
3701cb0ef41Sopenharmony_ciinstances of <a href="#class-replserver"><code>repl.REPLServer</code></a> will accept individual lines of user input,
3711cb0ef41Sopenharmony_cievaluate those according to a user-defined evaluation function, then output the
3721cb0ef41Sopenharmony_ciresult. Input and output may be from <code>stdin</code> and <code>stdout</code>, respectively, or may
3731cb0ef41Sopenharmony_cibe connected to any Node.js <a href="stream.html">stream</a>.</p>
3741cb0ef41Sopenharmony_ci<p>Instances of <a href="#class-replserver"><code>repl.REPLServer</code></a> support automatic completion of inputs,
3751cb0ef41Sopenharmony_cicompletion preview, simplistic Emacs-style line editing, multi-line inputs,
3761cb0ef41Sopenharmony_ci<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,
3771cb0ef41Sopenharmony_ciANSI-styled output, saving and restoring current REPL session state, error
3781cb0ef41Sopenharmony_cirecovery, and customizable evaluation functions. Terminals that do not support
3791cb0ef41Sopenharmony_ciANSI styles and Emacs-style line editing automatically fall back to a limited
3801cb0ef41Sopenharmony_cifeature set.</p>
3811cb0ef41Sopenharmony_ci<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>
3821cb0ef41Sopenharmony_ci<p>The following special commands are supported by all REPL instances:</p>
3831cb0ef41Sopenharmony_ci<ul>
3841cb0ef41Sopenharmony_ci<li><code>.break</code>: When in the process of inputting a multi-line expression, enter
3851cb0ef41Sopenharmony_cithe <code>.break</code> command (or press <kbd>Ctrl</kbd>+<kbd>C</kbd>) to abort
3861cb0ef41Sopenharmony_cifurther input or processing of that expression.</li>
3871cb0ef41Sopenharmony_ci<li><code>.clear</code>: Resets the REPL <code>context</code> to an empty object and clears any
3881cb0ef41Sopenharmony_cimulti-line expression being input.</li>
3891cb0ef41Sopenharmony_ci<li><code>.exit</code>: Close the I/O stream, causing the REPL to exit.</li>
3901cb0ef41Sopenharmony_ci<li><code>.help</code>: Show this list of special commands.</li>
3911cb0ef41Sopenharmony_ci<li><code>.save</code>: Save the current REPL session to a file:
3921cb0ef41Sopenharmony_ci<code>> .save ./file/to/save.js</code></li>
3931cb0ef41Sopenharmony_ci<li><code>.load</code>: Load a file into the current REPL session.
3941cb0ef41Sopenharmony_ci<code>> .load ./file/to/load.js</code></li>
3951cb0ef41Sopenharmony_ci<li><code>.editor</code>: Enter editor mode (<kbd>Ctrl</kbd>+<kbd>D</kbd> to
3961cb0ef41Sopenharmony_cifinish, <kbd>Ctrl</kbd>+<kbd>C</kbd> to cancel).</li>
3971cb0ef41Sopenharmony_ci</ul>
3981cb0ef41Sopenharmony_ci<pre><code class="language-console"><span class="hljs-meta prompt_">> </span><span class="language-bash">.editor</span>
3991cb0ef41Sopenharmony_ci// Entering editor mode (^D to finish, ^C to cancel)
4001cb0ef41Sopenharmony_cifunction welcome(name) {
4011cb0ef41Sopenharmony_ci  return `Hello ${name}!`;
4021cb0ef41Sopenharmony_ci}
4031cb0ef41Sopenharmony_ci
4041cb0ef41Sopenharmony_ciwelcome('Node.js User');
4051cb0ef41Sopenharmony_ci
4061cb0ef41Sopenharmony_ci// ^D
4071cb0ef41Sopenharmony_ci'Hello Node.js User!'
4081cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">></span></code> <button class="copy-button">copy</button></pre>
4091cb0ef41Sopenharmony_ci<p>The following key combinations in the REPL have these special effects:</p>
4101cb0ef41Sopenharmony_ci<ul>
4111cb0ef41Sopenharmony_ci<li><kbd>Ctrl</kbd>+<kbd>C</kbd>: When pressed once, has the same effect as the
4121cb0ef41Sopenharmony_ci<code>.break</code> command.
4131cb0ef41Sopenharmony_ciWhen pressed twice on a blank line, has the same effect as the <code>.exit</code>
4141cb0ef41Sopenharmony_cicommand.</li>
4151cb0ef41Sopenharmony_ci<li><kbd>Ctrl</kbd>+<kbd>D</kbd>: Has the same effect as the <code>.exit</code> command.</li>
4161cb0ef41Sopenharmony_ci<li><kbd>Tab</kbd>: When pressed on a blank line, displays global and local
4171cb0ef41Sopenharmony_ci(scope) variables. When pressed while entering other input, displays relevant
4181cb0ef41Sopenharmony_ciautocompletion options.</li>
4191cb0ef41Sopenharmony_ci</ul>
4201cb0ef41Sopenharmony_ci<p>For key bindings related to the reverse-i-search, see <a href="#reverse-i-search"><code>reverse-i-search</code></a>.
4211cb0ef41Sopenharmony_ciFor all other key bindings, see <a href="readline.html#tty-keybindings">TTY keybindings</a>.</p>
4221cb0ef41Sopenharmony_ci<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>
4231cb0ef41Sopenharmony_ci<p>By default, all instances of <a href="#class-replserver"><code>repl.REPLServer</code></a> use an evaluation function
4241cb0ef41Sopenharmony_cithat evaluates JavaScript expressions and provides access to Node.js built-in
4251cb0ef41Sopenharmony_cimodules. This default behavior can be overridden by passing in an alternative
4261cb0ef41Sopenharmony_cievaluation function when the <a href="#class-replserver"><code>repl.REPLServer</code></a> instance is created.</p>
4271cb0ef41Sopenharmony_ci<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>
4281cb0ef41Sopenharmony_ci<p>The default evaluator supports direct evaluation of JavaScript expressions:</p>
4291cb0ef41Sopenharmony_ci<pre><code class="language-console"><span class="hljs-meta prompt_">> </span><span class="language-bash">1 + 1</span>
4301cb0ef41Sopenharmony_ci2
4311cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">const m = 2</span>
4321cb0ef41Sopenharmony_ciundefined
4331cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">m + 1</span>
4341cb0ef41Sopenharmony_ci3</code> <button class="copy-button">copy</button></pre>
4351cb0ef41Sopenharmony_ci<p>Unless otherwise scoped within blocks or functions, variables declared
4361cb0ef41Sopenharmony_cieither implicitly or using the <code>const</code>, <code>let</code>, or <code>var</code> keywords
4371cb0ef41Sopenharmony_ciare declared at the global scope.</p>
4381cb0ef41Sopenharmony_ci<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>
4391cb0ef41Sopenharmony_ci<p>The default evaluator provides access to any variables that exist in the global
4401cb0ef41Sopenharmony_ciscope. It is possible to expose a variable to the REPL explicitly by assigning
4411cb0ef41Sopenharmony_ciit to the <code>context</code> object associated with each <code>REPLServer</code>:</p>
4421cb0ef41Sopenharmony_ci<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>);
4431cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> msg = <span class="hljs-string">'message'</span>;
4441cb0ef41Sopenharmony_ci
4451cb0ef41Sopenharmony_cirepl.<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> <button class="copy-button">copy</button></pre>
4461cb0ef41Sopenharmony_ci<p>Properties in the <code>context</code> object appear as local within the REPL:</p>
4471cb0ef41Sopenharmony_ci<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node repl_test.js</span>
4481cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">m</span>
4491cb0ef41Sopenharmony_ci'message'</code> <button class="copy-button">copy</button></pre>
4501cb0ef41Sopenharmony_ci<p>Context properties are not read-only by default. To specify read-only globals,
4511cb0ef41Sopenharmony_cicontext properties must be defined using <code>Object.defineProperty()</code>:</p>
4521cb0ef41Sopenharmony_ci<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>);
4531cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> msg = <span class="hljs-string">'message'</span>;
4541cb0ef41Sopenharmony_ci
4551cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>(<span class="hljs-string">'> '</span>);
4561cb0ef41Sopenharmony_ci<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>, {
4571cb0ef41Sopenharmony_ci  <span class="hljs-attr">configurable</span>: <span class="hljs-literal">false</span>,
4581cb0ef41Sopenharmony_ci  <span class="hljs-attr">enumerable</span>: <span class="hljs-literal">true</span>,
4591cb0ef41Sopenharmony_ci  <span class="hljs-attr">value</span>: msg,
4601cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
4611cb0ef41Sopenharmony_ci<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>
4621cb0ef41Sopenharmony_ci<p>The default evaluator will automatically load Node.js core modules into the
4631cb0ef41Sopenharmony_ciREPL environment when used. For instance, unless otherwise declared as a
4641cb0ef41Sopenharmony_ciglobal or scoped variable, the input <code>fs</code> will be evaluated on-demand as
4651cb0ef41Sopenharmony_ci<code>global.fs = require('node:fs')</code>.</p>
4661cb0ef41Sopenharmony_ci<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> <button class="copy-button">copy</button></pre>
4671cb0ef41Sopenharmony_ci<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>
4681cb0ef41Sopenharmony_ci<div class="api_metadata">
4691cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
4701cb0ef41Sopenharmony_ci<table>
4711cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
4721cb0ef41Sopenharmony_ci<tr><td>v12.3.0</td>
4731cb0ef41Sopenharmony_ci<td><p>The <code>'uncaughtException'</code> event is from now on triggered if the repl is used as standalone program.</p></td></tr>
4741cb0ef41Sopenharmony_ci</tbody></table>
4751cb0ef41Sopenharmony_ci</details>
4761cb0ef41Sopenharmony_ci</div>
4771cb0ef41Sopenharmony_ci<p>The REPL uses the <a href="domain.html"><code>domain</code></a> module to catch all uncaught exceptions for that
4781cb0ef41Sopenharmony_ciREPL session.</p>
4791cb0ef41Sopenharmony_ci<p>This use of the <a href="domain.html"><code>domain</code></a> module in the REPL has these side effects:</p>
4801cb0ef41Sopenharmony_ci<ul>
4811cb0ef41Sopenharmony_ci<li>
4821cb0ef41Sopenharmony_ci<p>Uncaught exceptions only emit the <a href="process.html#event-uncaughtexception"><code>'uncaughtException'</code></a> event in the
4831cb0ef41Sopenharmony_cistandalone REPL. Adding a listener for this event in a REPL within
4841cb0ef41Sopenharmony_cianother Node.js program results in <a href="errors.html#err_invalid_repl_input"><code>ERR_INVALID_REPL_INPUT</code></a>.</p>
4851cb0ef41Sopenharmony_ci<pre><code class="language-js"><span class="hljs-keyword">const</span> r = repl.<span class="hljs-title function_">start</span>();
4861cb0ef41Sopenharmony_ci
4871cb0ef41Sopenharmony_cir.<span class="hljs-title function_">write</span>(<span class="hljs-string">'process.on("uncaughtException", () => console.log("Foobar"));\n'</span>);
4881cb0ef41Sopenharmony_ci<span class="hljs-comment">// Output stream includes:</span>
4891cb0ef41Sopenharmony_ci<span class="hljs-comment">//   TypeError [ERR_INVALID_REPL_INPUT]: Listeners for `uncaughtException`</span>
4901cb0ef41Sopenharmony_ci<span class="hljs-comment">//   cannot be used in the REPL</span>
4911cb0ef41Sopenharmony_ci
4921cb0ef41Sopenharmony_cir.<span class="hljs-title function_">close</span>();</code> <button class="copy-button">copy</button></pre>
4931cb0ef41Sopenharmony_ci</li>
4941cb0ef41Sopenharmony_ci<li>
4951cb0ef41Sopenharmony_ci<p>Trying to use <a href="process.html#processsetuncaughtexceptioncapturecallbackfn"><code>process.setUncaughtExceptionCaptureCallback()</code></a> throws
4961cb0ef41Sopenharmony_cian <a href="errors.html#err_domain_cannot_set_uncaught_exception_capture"><code>ERR_DOMAIN_CANNOT_SET_UNCAUGHT_EXCEPTION_CAPTURE</code></a> error.</p>
4971cb0ef41Sopenharmony_ci</li>
4981cb0ef41Sopenharmony_ci</ul>
4991cb0ef41Sopenharmony_ci<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>
5001cb0ef41Sopenharmony_ci<div class="api_metadata">
5011cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
5021cb0ef41Sopenharmony_ci<table>
5031cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
5041cb0ef41Sopenharmony_ci<tr><td>v9.8.0</td>
5051cb0ef41Sopenharmony_ci<td><p>Added <code>_error</code> support.</p></td></tr>
5061cb0ef41Sopenharmony_ci</tbody></table>
5071cb0ef41Sopenharmony_ci</details>
5081cb0ef41Sopenharmony_ci</div>
5091cb0ef41Sopenharmony_ci<p>The default evaluator will, by default, assign the result of the most recently
5101cb0ef41Sopenharmony_cievaluated expression to the special variable <code>_</code> (underscore).
5111cb0ef41Sopenharmony_ciExplicitly setting <code>_</code> to a value will disable this behavior.</p>
5121cb0ef41Sopenharmony_ci<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>
5131cb0ef41Sopenharmony_ci[ 'a', 'b', 'c' ]
5141cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">_.length</span>
5151cb0ef41Sopenharmony_ci3
5161cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">_ += 1</span>
5171cb0ef41Sopenharmony_ciExpression assignment to _ now disabled.
5181cb0ef41Sopenharmony_ci4
5191cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">1 + 1</span>
5201cb0ef41Sopenharmony_ci2
5211cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">_</span>
5221cb0ef41Sopenharmony_ci4</code> <button class="copy-button">copy</button></pre>
5231cb0ef41Sopenharmony_ci<p>Similarly, <code>_error</code> will refer to the last seen error, if there was any.
5241cb0ef41Sopenharmony_ciExplicitly setting <code>_error</code> to a value will disable this behavior.</p>
5251cb0ef41Sopenharmony_ci<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>
5261cb0ef41Sopenharmony_ciUncaught Error: foo
5271cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">_error.message</span>
5281cb0ef41Sopenharmony_ci'foo'</code> <button class="copy-button">copy</button></pre>
5291cb0ef41Sopenharmony_ci<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>
5301cb0ef41Sopenharmony_ci<p>Support for the <code>await</code> keyword is enabled at the top level.</p>
5311cb0ef41Sopenharmony_ci<pre><code class="language-console"><span class="hljs-meta prompt_">> </span><span class="language-bash">await Promise.resolve(123)</span>
5321cb0ef41Sopenharmony_ci123
5331cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">await Promise.reject(new Error(<span class="hljs-string">'REPL await'</span>))</span>
5341cb0ef41Sopenharmony_ciUncaught Error: REPL await
5351cb0ef41Sopenharmony_ci    at REPL2:1:54
5361cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">const <span class="hljs-built_in">timeout</span> = util.promisify(setTimeout);</span>
5371cb0ef41Sopenharmony_ciundefined
5381cb0ef41Sopenharmony_ci<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>
5391cb0ef41Sopenharmony_ci1002
5401cb0ef41Sopenharmony_ciundefined</code> <button class="copy-button">copy</button></pre>
5411cb0ef41Sopenharmony_ci<p>One known limitation of using the <code>await</code> keyword in the REPL is that
5421cb0ef41Sopenharmony_ciit will invalidate the lexical scoping of the <code>const</code> and <code>let</code>
5431cb0ef41Sopenharmony_cikeywords.</p>
5441cb0ef41Sopenharmony_ci<p>For example:</p>
5451cb0ef41Sopenharmony_ci<pre><code class="language-console"><span class="hljs-meta prompt_">> </span><span class="language-bash">const m = await Promise.resolve(123)</span>
5461cb0ef41Sopenharmony_ciundefined
5471cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">m</span>
5481cb0ef41Sopenharmony_ci123
5491cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">const m = await Promise.resolve(234)</span>
5501cb0ef41Sopenharmony_ciundefined
5511cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">m</span>
5521cb0ef41Sopenharmony_ci234</code> <button class="copy-button">copy</button></pre>
5531cb0ef41Sopenharmony_ci<p><a href="cli.html#--no-experimental-repl-await"><code>--no-experimental-repl-await</code></a> shall disable top-level await in REPL.</p>
5541cb0ef41Sopenharmony_ci<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>
5551cb0ef41Sopenharmony_ci<div class="api_metadata">
5561cb0ef41Sopenharmony_ci<span>Added in: v13.6.0, v12.17.0</span>
5571cb0ef41Sopenharmony_ci</div>
5581cb0ef41Sopenharmony_ci<p>The REPL supports bi-directional reverse-i-search similar to <a href="https://en.wikipedia.org/wiki/Z_shell">ZSH</a>. It is
5591cb0ef41Sopenharmony_citriggered with <kbd>Ctrl</kbd>+<kbd>R</kbd> to search backward
5601cb0ef41Sopenharmony_ciand <kbd>Ctrl</kbd>+<kbd>S</kbd> to search forwards.</p>
5611cb0ef41Sopenharmony_ci<p>Duplicated history entries will be skipped.</p>
5621cb0ef41Sopenharmony_ci<p>Entries are accepted as soon as any key is pressed that doesn't correspond
5631cb0ef41Sopenharmony_ciwith the reverse search. Cancelling is possible by pressing <kbd>Esc</kbd>
5641cb0ef41Sopenharmony_cior <kbd>Ctrl</kbd>+<kbd>C</kbd>.</p>
5651cb0ef41Sopenharmony_ci<p>Changing the direction immediately searches for the next entry in the expected
5661cb0ef41Sopenharmony_cidirection from the current position on.</p>
5671cb0ef41Sopenharmony_ci<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>
5681cb0ef41Sopenharmony_ci<p>When a new <a href="#class-replserver"><code>repl.REPLServer</code></a> is created, a custom evaluation function may be
5691cb0ef41Sopenharmony_ciprovided. This can be used, for instance, to implement fully customized REPL
5701cb0ef41Sopenharmony_ciapplications.</p>
5711cb0ef41Sopenharmony_ci<p>The following illustrates a hypothetical example of a REPL that performs
5721cb0ef41Sopenharmony_citranslation of text from one language to another:</p>
5731cb0ef41Sopenharmony_ci<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>);
5741cb0ef41Sopenharmony_ci<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>);
5751cb0ef41Sopenharmony_ci
5761cb0ef41Sopenharmony_ci<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>);
5771cb0ef41Sopenharmony_ci
5781cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">myEval</span>(<span class="hljs-params">cmd, context, filename, callback</span>) {
5791cb0ef41Sopenharmony_ci  <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, myTranslator.<span class="hljs-title function_">translate</span>(cmd));
5801cb0ef41Sopenharmony_ci}
5811cb0ef41Sopenharmony_ci
5821cb0ef41Sopenharmony_cirepl.<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> <button class="copy-button">copy</button></pre>
5831cb0ef41Sopenharmony_ci<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>
5841cb0ef41Sopenharmony_ci<p>At the REPL prompt, pressing <kbd>Enter</kbd> sends the current line of input to
5851cb0ef41Sopenharmony_cithe <code>eval</code> function. In order to support multi-line input, the <code>eval</code> function
5861cb0ef41Sopenharmony_cican return an instance of <code>repl.Recoverable</code> to the provided callback function:</p>
5871cb0ef41Sopenharmony_ci<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>) {
5881cb0ef41Sopenharmony_ci  <span class="hljs-keyword">let</span> result;
5891cb0ef41Sopenharmony_ci  <span class="hljs-keyword">try</span> {
5901cb0ef41Sopenharmony_ci    result = vm.<span class="hljs-title function_">runInThisContext</span>(cmd);
5911cb0ef41Sopenharmony_ci  } <span class="hljs-keyword">catch</span> (e) {
5921cb0ef41Sopenharmony_ci    <span class="hljs-keyword">if</span> (<span class="hljs-title function_">isRecoverableError</span>(e)) {
5931cb0ef41Sopenharmony_ci      <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));
5941cb0ef41Sopenharmony_ci    }
5951cb0ef41Sopenharmony_ci  }
5961cb0ef41Sopenharmony_ci  <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, result);
5971cb0ef41Sopenharmony_ci}
5981cb0ef41Sopenharmony_ci
5991cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">isRecoverableError</span>(<span class="hljs-params">error</span>) {
6001cb0ef41Sopenharmony_ci  <span class="hljs-keyword">if</span> (error.<span class="hljs-property">name</span> === <span class="hljs-string">'SyntaxError'</span>) {
6011cb0ef41Sopenharmony_ci    <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>);
6021cb0ef41Sopenharmony_ci  }
6031cb0ef41Sopenharmony_ci  <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
6041cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
6051cb0ef41Sopenharmony_ci<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>
6061cb0ef41Sopenharmony_ci<p>By default, <a href="#class-replserver"><code>repl.REPLServer</code></a> instances format output using the
6071cb0ef41Sopenharmony_ci<a href="util.html#utilinspectobject-options"><code>util.inspect()</code></a> method before writing the output to the provided <code>Writable</code>
6081cb0ef41Sopenharmony_cistream (<code>process.stdout</code> by default). The <code>showProxy</code> inspection option is set
6091cb0ef41Sopenharmony_cito true by default and the <code>colors</code> option is set to true depending on the
6101cb0ef41Sopenharmony_ciREPL's <code>useColors</code> option.</p>
6111cb0ef41Sopenharmony_ci<p>The <code>useColors</code> boolean option can be specified at construction to instruct the
6121cb0ef41Sopenharmony_cidefault writer to use ANSI style codes to colorize the output from the
6131cb0ef41Sopenharmony_ci<code>util.inspect()</code> method.</p>
6141cb0ef41Sopenharmony_ci<p>If the REPL is run as standalone program, it is also possible to change the
6151cb0ef41Sopenharmony_ciREPL's <a href="util.html#utilinspectobject-options">inspection defaults</a> from inside the REPL by using the
6161cb0ef41Sopenharmony_ci<code>inspect.replDefaults</code> property which mirrors the <code>defaultOptions</code> from
6171cb0ef41Sopenharmony_ci<a href="util.html#utilinspectobject-options"><code>util.inspect()</code></a>.</p>
6181cb0ef41Sopenharmony_ci<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>
6191cb0ef41Sopenharmony_cifalse
6201cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">[1]</span>
6211cb0ef41Sopenharmony_ci[
6221cb0ef41Sopenharmony_ci  1
6231cb0ef41Sopenharmony_ci]
6241cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">></span></code> <button class="copy-button">copy</button></pre>
6251cb0ef41Sopenharmony_ci<p>To fully customize the output of a <a href="#class-replserver"><code>repl.REPLServer</code></a> instance pass in a new
6261cb0ef41Sopenharmony_cifunction for the <code>writer</code> option on construction. The following example, for
6271cb0ef41Sopenharmony_ciinstance, simply converts any input text to upper case:</p>
6281cb0ef41Sopenharmony_ci<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>);
6291cb0ef41Sopenharmony_ci
6301cb0ef41Sopenharmony_ci<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 });
6311cb0ef41Sopenharmony_ci
6321cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">myEval</span>(<span class="hljs-params">cmd, context, filename, callback</span>) {
6331cb0ef41Sopenharmony_ci  <span class="hljs-title function_">callback</span>(<span class="hljs-literal">null</span>, cmd);
6341cb0ef41Sopenharmony_ci}
6351cb0ef41Sopenharmony_ci
6361cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">myWriter</span>(<span class="hljs-params">output</span>) {
6371cb0ef41Sopenharmony_ci  <span class="hljs-keyword">return</span> output.<span class="hljs-title function_">toUpperCase</span>();
6381cb0ef41Sopenharmony_ci}</code> <button class="copy-button">copy</button></pre>
6391cb0ef41Sopenharmony_ci</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>
6401cb0ef41Sopenharmony_ci<div class="api_metadata">
6411cb0ef41Sopenharmony_ci<span>Added in: v0.1.91</span>
6421cb0ef41Sopenharmony_ci</div>
6431cb0ef41Sopenharmony_ci<ul>
6441cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> See <a href="#replstartoptions"><code>repl.start()</code></a></li>
6451cb0ef41Sopenharmony_ci<li>Extends: <a href="readline.html#class-readlineinterface" class="type">&#x3C;readline.Interface></a></li>
6461cb0ef41Sopenharmony_ci</ul>
6471cb0ef41Sopenharmony_ci<p>Instances of <code>repl.REPLServer</code> are created using the <a href="#replstartoptions"><code>repl.start()</code></a> method
6481cb0ef41Sopenharmony_cior directly using the JavaScript <code>new</code> keyword.</p>
6491cb0ef41Sopenharmony_ci<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>);
6501cb0ef41Sopenharmony_ci
6511cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> options = { <span class="hljs-attr">useColors</span>: <span class="hljs-literal">true</span> };
6521cb0ef41Sopenharmony_ci
6531cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> firstInstance = repl.<span class="hljs-title function_">start</span>(options);
6541cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> secondInstance = <span class="hljs-keyword">new</span> repl.<span class="hljs-title class_">REPLServer</span>(options);</code> <button class="copy-button">copy</button></pre>
6551cb0ef41Sopenharmony_ci<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>
6561cb0ef41Sopenharmony_ci<div class="api_metadata">
6571cb0ef41Sopenharmony_ci<span>Added in: v0.7.7</span>
6581cb0ef41Sopenharmony_ci</div>
6591cb0ef41Sopenharmony_ci<p>The <code>'exit'</code> event is emitted when the REPL is exited either by receiving the
6601cb0ef41Sopenharmony_ci<code>.exit</code> command as input, the user pressing <kbd>Ctrl</kbd>+<kbd>C</kbd> twice
6611cb0ef41Sopenharmony_cito signal <code>SIGINT</code>,
6621cb0ef41Sopenharmony_cior by pressing <kbd>Ctrl</kbd>+<kbd>D</kbd> to signal <code>'end'</code> on the input
6631cb0ef41Sopenharmony_cistream. The listener
6641cb0ef41Sopenharmony_cicallback is invoked without any arguments.</p>
6651cb0ef41Sopenharmony_ci<pre><code class="language-js">replServer.<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">() =></span> {
6661cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Received "exit" event from repl!'</span>);
6671cb0ef41Sopenharmony_ci  process.<span class="hljs-title function_">exit</span>();
6681cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
6691cb0ef41Sopenharmony_ci<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>
6701cb0ef41Sopenharmony_ci<div class="api_metadata">
6711cb0ef41Sopenharmony_ci<span>Added in: v0.11.0</span>
6721cb0ef41Sopenharmony_ci</div>
6731cb0ef41Sopenharmony_ci<p>The <code>'reset'</code> event is emitted when the REPL's context is reset. This occurs
6741cb0ef41Sopenharmony_ciwhenever the <code>.clear</code> command is received as input <em>unless</em> the REPL is using
6751cb0ef41Sopenharmony_cithe default evaluator and the <code>repl.REPLServer</code> instance was created with the
6761cb0ef41Sopenharmony_ci<code>useGlobal</code> option set to <code>true</code>. The listener callback will be called with a
6771cb0ef41Sopenharmony_cireference to the <code>context</code> object as the only argument.</p>
6781cb0ef41Sopenharmony_ci<p>This can be used primarily to re-initialize REPL context to some pre-defined
6791cb0ef41Sopenharmony_cistate:</p>
6801cb0ef41Sopenharmony_ci<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>);
6811cb0ef41Sopenharmony_ci
6821cb0ef41Sopenharmony_ci<span class="hljs-keyword">function</span> <span class="hljs-title function_">initializeContext</span>(<span class="hljs-params">context</span>) {
6831cb0ef41Sopenharmony_ci  context.<span class="hljs-property">m</span> = <span class="hljs-string">'test'</span>;
6841cb0ef41Sopenharmony_ci}
6851cb0ef41Sopenharmony_ci
6861cb0ef41Sopenharmony_ci<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> });
6871cb0ef41Sopenharmony_ci<span class="hljs-title function_">initializeContext</span>(r.<span class="hljs-property">context</span>);
6881cb0ef41Sopenharmony_ci
6891cb0ef41Sopenharmony_cir.<span class="hljs-title function_">on</span>(<span class="hljs-string">'reset'</span>, initializeContext);</code> <button class="copy-button">copy</button></pre>
6901cb0ef41Sopenharmony_ci<p>When this code is executed, the global <code>'m'</code> variable can be modified but then
6911cb0ef41Sopenharmony_cireset to its initial value using the <code>.clear</code> command:</p>
6921cb0ef41Sopenharmony_ci<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">./node example.js</span>
6931cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">m</span>
6941cb0ef41Sopenharmony_ci'test'
6951cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">m = 1</span>
6961cb0ef41Sopenharmony_ci1
6971cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">m</span>
6981cb0ef41Sopenharmony_ci1
6991cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">.clear</span>
7001cb0ef41Sopenharmony_ciClearing context...
7011cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">m</span>
7021cb0ef41Sopenharmony_ci'test'
7031cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">></span></code> <button class="copy-button">copy</button></pre>
7041cb0ef41Sopenharmony_ci<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>
7051cb0ef41Sopenharmony_ci<div class="api_metadata">
7061cb0ef41Sopenharmony_ci<span>Added in: v0.3.0</span>
7071cb0ef41Sopenharmony_ci</div>
7081cb0ef41Sopenharmony_ci<ul>
7091cb0ef41Sopenharmony_ci<li><code>keyword</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The command keyword (<em>without</em> a leading <code>.</code> character).</li>
7101cb0ef41Sopenharmony_ci<li><code>cmd</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The function to invoke when the command is processed.</li>
7111cb0ef41Sopenharmony_ci</ul>
7121cb0ef41Sopenharmony_ci<p>The <code>replServer.defineCommand()</code> method is used to add new <code>.</code>-prefixed commands
7131cb0ef41Sopenharmony_cito the REPL instance. Such commands are invoked by typing a <code>.</code> followed by the
7141cb0ef41Sopenharmony_ci<code>keyword</code>. The <code>cmd</code> is either a <code>Function</code> or an <code>Object</code> with the following
7151cb0ef41Sopenharmony_ciproperties:</p>
7161cb0ef41Sopenharmony_ci<ul>
7171cb0ef41Sopenharmony_ci<li><code>help</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Help text to be displayed when <code>.help</code> is entered (Optional).</li>
7181cb0ef41Sopenharmony_ci<li><code>action</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The function to execute, optionally accepting a single
7191cb0ef41Sopenharmony_cistring argument.</li>
7201cb0ef41Sopenharmony_ci</ul>
7211cb0ef41Sopenharmony_ci<p>The following example shows two new commands added to the REPL instance:</p>
7221cb0ef41Sopenharmony_ci<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>);
7231cb0ef41Sopenharmony_ci
7241cb0ef41Sopenharmony_ci<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> });
7251cb0ef41Sopenharmony_cireplServer.<span class="hljs-title function_">defineCommand</span>(<span class="hljs-string">'sayhello'</span>, {
7261cb0ef41Sopenharmony_ci  <span class="hljs-attr">help</span>: <span class="hljs-string">'Say hello'</span>,
7271cb0ef41Sopenharmony_ci  <span class="hljs-title function_">action</span>(<span class="hljs-params">name</span>) {
7281cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">clearBufferedCommand</span>();
7291cb0ef41Sopenharmony_ci    <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>);
7301cb0ef41Sopenharmony_ci    <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">displayPrompt</span>();
7311cb0ef41Sopenharmony_ci  },
7321cb0ef41Sopenharmony_ci});
7331cb0ef41Sopenharmony_cireplServer.<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>) {
7341cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Goodbye!'</span>);
7351cb0ef41Sopenharmony_ci  <span class="hljs-variable language_">this</span>.<span class="hljs-title function_">close</span>();
7361cb0ef41Sopenharmony_ci});</code> <button class="copy-button">copy</button></pre>
7371cb0ef41Sopenharmony_ci<p>The new commands can then be used from within the REPL instance:</p>
7381cb0ef41Sopenharmony_ci<pre><code class="language-console"><span class="hljs-meta prompt_">> </span><span class="language-bash">.sayhello Node.js User</span>
7391cb0ef41Sopenharmony_ciHello, Node.js User!
7401cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">.saybye</span>
7411cb0ef41Sopenharmony_ciGoodbye!</code> <button class="copy-button">copy</button></pre>
7421cb0ef41Sopenharmony_ci<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>
7431cb0ef41Sopenharmony_ci<div class="api_metadata">
7441cb0ef41Sopenharmony_ci<span>Added in: v0.1.91</span>
7451cb0ef41Sopenharmony_ci</div>
7461cb0ef41Sopenharmony_ci<ul>
7471cb0ef41Sopenharmony_ci<li><code>preserveCursor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
7481cb0ef41Sopenharmony_ci</ul>
7491cb0ef41Sopenharmony_ci<p>The <code>replServer.displayPrompt()</code> method readies the REPL instance for input
7501cb0ef41Sopenharmony_cifrom the user, printing the configured <code>prompt</code> to a new line in the <code>output</code>
7511cb0ef41Sopenharmony_ciand resuming the <code>input</code> to accept new input.</p>
7521cb0ef41Sopenharmony_ci<p>When multi-line input is being entered, an ellipsis is printed rather than the
7531cb0ef41Sopenharmony_ci'prompt'.</p>
7541cb0ef41Sopenharmony_ci<p>When <code>preserveCursor</code> is <code>true</code>, the cursor placement will not be reset to <code>0</code>.</p>
7551cb0ef41Sopenharmony_ci<p>The <code>replServer.displayPrompt</code> method is primarily intended to be called from
7561cb0ef41Sopenharmony_ciwithin the action function for commands registered using the
7571cb0ef41Sopenharmony_ci<code>replServer.defineCommand()</code> method.</p>
7581cb0ef41Sopenharmony_ci<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>
7591cb0ef41Sopenharmony_ci<div class="api_metadata">
7601cb0ef41Sopenharmony_ci<span>Added in: v9.0.0</span>
7611cb0ef41Sopenharmony_ci</div>
7621cb0ef41Sopenharmony_ci<p>The <code>replServer.clearBufferedCommand()</code> method clears any command that has been
7631cb0ef41Sopenharmony_cibuffered but not yet executed. This method is primarily intended to be
7641cb0ef41Sopenharmony_cicalled from within the action function for commands registered using the
7651cb0ef41Sopenharmony_ci<code>replServer.defineCommand()</code> method.</p>
7661cb0ef41Sopenharmony_ci<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>
7671cb0ef41Sopenharmony_ci<div class="api_metadata">
7681cb0ef41Sopenharmony_ci<span>Added in: v0.8.9</span><span>Deprecated since: v9.0.0</span>
7691cb0ef41Sopenharmony_ci</div>
7701cb0ef41Sopenharmony_ci<p></p><div class="api_stability api_stability_0"><a href="documentation.html#stability-index">Stability: 0</a> - Deprecated.</div><p></p>
7711cb0ef41Sopenharmony_ci<ul>
7721cb0ef41Sopenharmony_ci<li><code>keyword</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> the potential keyword to parse and execute</li>
7731cb0ef41Sopenharmony_ci<li><code>rest</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type">&#x3C;any></a> any parameters to the keyword command</li>
7741cb0ef41Sopenharmony_ci<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a></li>
7751cb0ef41Sopenharmony_ci</ul>
7761cb0ef41Sopenharmony_ci<p>An internal method used to parse and execute <code>REPLServer</code> keywords.
7771cb0ef41Sopenharmony_ciReturns <code>true</code> if <code>keyword</code> is a valid keyword, otherwise <code>false</code>.</p>
7781cb0ef41Sopenharmony_ci<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>
7791cb0ef41Sopenharmony_ci<div class="api_metadata">
7801cb0ef41Sopenharmony_ci<span>Added in: v11.10.0</span>
7811cb0ef41Sopenharmony_ci</div>
7821cb0ef41Sopenharmony_ci<ul>
7831cb0ef41Sopenharmony_ci<li><code>historyPath</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> the path to the history file</li>
7841cb0ef41Sopenharmony_ci<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> called when history writes are ready or upon error
7851cb0ef41Sopenharmony_ci<ul>
7861cb0ef41Sopenharmony_ci<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error" class="type">&#x3C;Error></a></li>
7871cb0ef41Sopenharmony_ci<li><code>repl</code> <a href="repl.html#class-replserver" class="type">&#x3C;repl.REPLServer></a></li>
7881cb0ef41Sopenharmony_ci</ul>
7891cb0ef41Sopenharmony_ci</li>
7901cb0ef41Sopenharmony_ci</ul>
7911cb0ef41Sopenharmony_ci<p>Initializes a history log file for the REPL instance. When executing the
7921cb0ef41Sopenharmony_ciNode.js binary and using the command-line REPL, a history file is initialized
7931cb0ef41Sopenharmony_ciby default. However, this is not the case when creating a REPL
7941cb0ef41Sopenharmony_ciprogrammatically. Use this method to initialize a history log file when working
7951cb0ef41Sopenharmony_ciwith REPL instances programmatically.</p>
7961cb0ef41Sopenharmony_ci</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>
7971cb0ef41Sopenharmony_ci<div class="api_metadata">
7981cb0ef41Sopenharmony_ci<span>Added in: v14.5.0</span>
7991cb0ef41Sopenharmony_ci</div>
8001cb0ef41Sopenharmony_ci<ul>
8011cb0ef41Sopenharmony_ci<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string[]></a></li>
8021cb0ef41Sopenharmony_ci</ul>
8031cb0ef41Sopenharmony_ci<p>A list of the names of all Node.js modules, e.g., <code>'http'</code>.</p>
8041cb0ef41Sopenharmony_ci</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>
8051cb0ef41Sopenharmony_ci<div class="api_metadata">
8061cb0ef41Sopenharmony_ci<details class="changelog"><summary>History</summary>
8071cb0ef41Sopenharmony_ci<table>
8081cb0ef41Sopenharmony_ci<tbody><tr><th>Version</th><th>Changes</th></tr>
8091cb0ef41Sopenharmony_ci<tr><td>v13.4.0, v12.17.0</td>
8101cb0ef41Sopenharmony_ci<td><p>The <code>preview</code> option is now available.</p></td></tr>
8111cb0ef41Sopenharmony_ci<tr><td>v12.0.0</td>
8121cb0ef41Sopenharmony_ci<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>
8131cb0ef41Sopenharmony_ci<tr><td>v10.0.0</td>
8141cb0ef41Sopenharmony_ci<td><p>The <code>REPL_MAGIC_MODE</code> <code>replMode</code> was removed.</p></td></tr>
8151cb0ef41Sopenharmony_ci<tr><td>v6.3.0</td>
8161cb0ef41Sopenharmony_ci<td><p>The <code>breakEvalOnSigint</code> option is supported now.</p></td></tr>
8171cb0ef41Sopenharmony_ci<tr><td>v5.8.0</td>
8181cb0ef41Sopenharmony_ci<td><p>The <code>options</code> parameter is optional now.</p></td></tr>
8191cb0ef41Sopenharmony_ci<tr><td>v0.1.91</td>
8201cb0ef41Sopenharmony_ci<td><p><span>Added in: v0.1.91</span></p></td></tr>
8211cb0ef41Sopenharmony_ci</tbody></table>
8221cb0ef41Sopenharmony_ci</details>
8231cb0ef41Sopenharmony_ci</div>
8241cb0ef41Sopenharmony_ci<ul>
8251cb0ef41Sopenharmony_ci<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a>
8261cb0ef41Sopenharmony_ci<ul>
8271cb0ef41Sopenharmony_ci<li><code>prompt</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> The input prompt to display. <strong>Default:</strong> <code>'> '</code>
8281cb0ef41Sopenharmony_ci(with a trailing space).</li>
8291cb0ef41Sopenharmony_ci<li><code>input</code> <a href="stream.html#class-streamreadable" class="type">&#x3C;stream.Readable></a> The <code>Readable</code> stream from which REPL input will
8301cb0ef41Sopenharmony_cibe read. <strong>Default:</strong> <code>process.stdin</code>.</li>
8311cb0ef41Sopenharmony_ci<li><code>output</code> <a href="stream.html#class-streamwritable" class="type">&#x3C;stream.Writable></a> The <code>Writable</code> stream to which REPL output will
8321cb0ef41Sopenharmony_cibe written. <strong>Default:</strong> <code>process.stdout</code>.</li>
8331cb0ef41Sopenharmony_ci<li><code>terminal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, specifies that the <code>output</code> should be
8341cb0ef41Sopenharmony_citreated as a TTY terminal.
8351cb0ef41Sopenharmony_ci<strong>Default:</strong> checking the value of the <code>isTTY</code> property on the <code>output</code>
8361cb0ef41Sopenharmony_cistream upon instantiation.</li>
8371cb0ef41Sopenharmony_ci<li><code>eval</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The function to be used when evaluating each given line
8381cb0ef41Sopenharmony_ciof input. <strong>Default:</strong> an async wrapper for the JavaScript <code>eval()</code>
8391cb0ef41Sopenharmony_cifunction. An <code>eval</code> function can error with <code>repl.Recoverable</code> to indicate
8401cb0ef41Sopenharmony_cithe input was incomplete and prompt for additional lines.</li>
8411cb0ef41Sopenharmony_ci<li><code>useColors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, specifies that the default <code>writer</code>
8421cb0ef41Sopenharmony_cifunction should include ANSI color styling to REPL output. If a custom
8431cb0ef41Sopenharmony_ci<code>writer</code> function is provided then this has no effect. <strong>Default:</strong> checking
8441cb0ef41Sopenharmony_cicolor support on the <code>output</code> stream if the REPL instance's <code>terminal</code> value
8451cb0ef41Sopenharmony_ciis <code>true</code>.</li>
8461cb0ef41Sopenharmony_ci<li><code>useGlobal</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, specifies that the default evaluation
8471cb0ef41Sopenharmony_cifunction will use the JavaScript <code>global</code> as the context as opposed to
8481cb0ef41Sopenharmony_cicreating a new separate context for the REPL instance. The node CLI REPL
8491cb0ef41Sopenharmony_cisets this value to <code>true</code>. <strong>Default:</strong> <code>false</code>.</li>
8501cb0ef41Sopenharmony_ci<li><code>ignoreUndefined</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> If <code>true</code>, specifies that the default writer
8511cb0ef41Sopenharmony_ciwill not output the return value of a command if it evaluates to
8521cb0ef41Sopenharmony_ci<code>undefined</code>. <strong>Default:</strong> <code>false</code>.</li>
8531cb0ef41Sopenharmony_ci<li><code>writer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> The function to invoke to format the output of each
8541cb0ef41Sopenharmony_cicommand before writing to <code>output</code>. <strong>Default:</strong> <a href="util.html#utilinspectobject-options"><code>util.inspect()</code></a>.</li>
8551cb0ef41Sopenharmony_ci<li><code>completer</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a> An optional function used for custom Tab auto
8561cb0ef41Sopenharmony_cicompletion. See <a href="readline.html#use-of-the-completer-function"><code>readline.InterfaceCompleter</code></a> for an example.</li>
8571cb0ef41Sopenharmony_ci<li><code>replMode</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type">&#x3C;symbol></a> A flag that specifies whether the default evaluator
8581cb0ef41Sopenharmony_ciexecutes all JavaScript commands in strict mode or default (sloppy) mode.
8591cb0ef41Sopenharmony_ciAcceptable values are:
8601cb0ef41Sopenharmony_ci<ul>
8611cb0ef41Sopenharmony_ci<li><code>repl.REPL_MODE_SLOPPY</code> to evaluate expressions in sloppy mode.</li>
8621cb0ef41Sopenharmony_ci<li><code>repl.REPL_MODE_STRICT</code> to evaluate expressions in strict mode. This is
8631cb0ef41Sopenharmony_ciequivalent to prefacing every repl statement with <code>'use strict'</code>.</li>
8641cb0ef41Sopenharmony_ci</ul>
8651cb0ef41Sopenharmony_ci</li>
8661cb0ef41Sopenharmony_ci<li><code>breakEvalOnSigint</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Stop evaluating the current piece of code when
8671cb0ef41Sopenharmony_ci<code>SIGINT</code> is received, such as when <kbd>Ctrl</kbd>+<kbd>C</kbd> is pressed.
8681cb0ef41Sopenharmony_ciThis cannot be used
8691cb0ef41Sopenharmony_citogether with a custom <code>eval</code> function. <strong>Default:</strong> <code>false</code>.</li>
8701cb0ef41Sopenharmony_ci<li><code>preview</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Defines if the repl prints autocomplete and output
8711cb0ef41Sopenharmony_cipreviews or not. <strong>Default:</strong> <code>true</code> with the default eval function and
8721cb0ef41Sopenharmony_ci<code>false</code> in case a custom eval function is used. If <code>terminal</code> is falsy, then
8731cb0ef41Sopenharmony_cithere are no previews and the value of <code>preview</code> has no effect.</li>
8741cb0ef41Sopenharmony_ci</ul>
8751cb0ef41Sopenharmony_ci</li>
8761cb0ef41Sopenharmony_ci<li>Returns: <a href="repl.html#class-replserver" class="type">&#x3C;repl.REPLServer></a></li>
8771cb0ef41Sopenharmony_ci</ul>
8781cb0ef41Sopenharmony_ci<p>The <code>repl.start()</code> method creates and starts a <a href="#class-replserver"><code>repl.REPLServer</code></a> instance.</p>
8791cb0ef41Sopenharmony_ci<p>If <code>options</code> is a string, then it specifies the input prompt:</p>
8801cb0ef41Sopenharmony_ci<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>);
8811cb0ef41Sopenharmony_ci
8821cb0ef41Sopenharmony_ci<span class="hljs-comment">// a Unix style prompt</span>
8831cb0ef41Sopenharmony_cirepl.<span class="hljs-title function_">start</span>(<span class="hljs-string">'$ '</span>);</code> <button class="copy-button">copy</button></pre>
8841cb0ef41Sopenharmony_ci</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>
8851cb0ef41Sopenharmony_ci<p>Node.js itself uses the <code>node:repl</code> module to provide its own interactive
8861cb0ef41Sopenharmony_ciinterface for executing JavaScript. This can be used by executing the Node.js
8871cb0ef41Sopenharmony_cibinary without passing any arguments (or by passing the <code>-i</code> argument):</p>
8881cb0ef41Sopenharmony_ci<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node</span>
8891cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">const a = [1, 2, 3];</span>
8901cb0ef41Sopenharmony_ciundefined
8911cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">a</span>
8921cb0ef41Sopenharmony_ci[ 1, 2, 3 ]
8931cb0ef41Sopenharmony_ci<span class="hljs-meta prompt_">> </span><span class="language-bash">a.forEach((v) => {</span>
8941cb0ef41Sopenharmony_ci...   console.log(v);
8951cb0ef41Sopenharmony_ci...   });
8961cb0ef41Sopenharmony_ci1
8971cb0ef41Sopenharmony_ci2
8981cb0ef41Sopenharmony_ci3</code> <button class="copy-button">copy</button></pre>
8991cb0ef41Sopenharmony_ci<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>
9001cb0ef41Sopenharmony_ci<p>Various behaviors of the Node.js REPL can be customized using the following
9011cb0ef41Sopenharmony_cienvironment variables:</p>
9021cb0ef41Sopenharmony_ci<ul>
9031cb0ef41Sopenharmony_ci<li><code>NODE_REPL_HISTORY</code>: When a valid path is given, persistent REPL history
9041cb0ef41Sopenharmony_ciwill be saved to the specified file rather than <code>.node_repl_history</code> in the
9051cb0ef41Sopenharmony_ciuser's home directory. Setting this value to <code>''</code> (an empty string) will
9061cb0ef41Sopenharmony_cidisable persistent REPL history. Whitespace will be trimmed from the value.
9071cb0ef41Sopenharmony_ciOn Windows platforms environment variables with empty values are invalid so
9081cb0ef41Sopenharmony_ciset this variable to one or more spaces to disable persistent REPL history.</li>
9091cb0ef41Sopenharmony_ci<li><code>NODE_REPL_HISTORY_SIZE</code>: Controls how many lines of history will be
9101cb0ef41Sopenharmony_cipersisted if history is available. Must be a positive number.
9111cb0ef41Sopenharmony_ci<strong>Default:</strong> <code>1000</code>.</li>
9121cb0ef41Sopenharmony_ci<li><code>NODE_REPL_MODE</code>: May be either <code>'sloppy'</code> or <code>'strict'</code>. <strong>Default:</strong>
9131cb0ef41Sopenharmony_ci<code>'sloppy'</code>, which will allow non-strict mode code to be run.</li>
9141cb0ef41Sopenharmony_ci</ul>
9151cb0ef41Sopenharmony_ci<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>
9161cb0ef41Sopenharmony_ci<p>By default, the Node.js REPL will persist history between <code>node</code> REPL sessions
9171cb0ef41Sopenharmony_ciby saving inputs to a <code>.node_repl_history</code> file located in the user's home
9181cb0ef41Sopenharmony_cidirectory. This can be disabled by setting the environment variable
9191cb0ef41Sopenharmony_ci<code>NODE_REPL_HISTORY=''</code>.</p>
9201cb0ef41Sopenharmony_ci<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>
9211cb0ef41Sopenharmony_ci<p>For advanced line-editors, start Node.js with the environment variable
9221cb0ef41Sopenharmony_ci<code>NODE_NO_READLINE=1</code>. This will start the main and debugger REPL in canonical
9231cb0ef41Sopenharmony_citerminal settings, which will allow use with <code>rlwrap</code>.</p>
9241cb0ef41Sopenharmony_ci<p>For example, the following can be added to a <code>.bashrc</code> file:</p>
9251cb0ef41Sopenharmony_ci<pre><code class="language-text">alias node="env NODE_NO_READLINE=1 rlwrap node"</code> <button class="copy-button">copy</button></pre>
9261cb0ef41Sopenharmony_ci<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>
9271cb0ef41Sopenharmony_ci<p>It is possible to create and run multiple REPL instances against a single
9281cb0ef41Sopenharmony_cirunning instance of Node.js that share a single <code>global</code> object but have
9291cb0ef41Sopenharmony_ciseparate I/O interfaces.</p>
9301cb0ef41Sopenharmony_ci<p>The following example, for instance, provides separate REPLs on <code>stdin</code>, a Unix
9311cb0ef41Sopenharmony_cisocket, and a TCP socket:</p>
9321cb0ef41Sopenharmony_ci<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>);
9331cb0ef41Sopenharmony_ci<span class="hljs-keyword">const</span> repl = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:repl'</span>);
9341cb0ef41Sopenharmony_ci<span class="hljs-keyword">let</span> connections = <span class="hljs-number">0</span>;
9351cb0ef41Sopenharmony_ci
9361cb0ef41Sopenharmony_cirepl.<span class="hljs-title function_">start</span>({
9371cb0ef41Sopenharmony_ci  <span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via stdin> '</span>,
9381cb0ef41Sopenharmony_ci  <span class="hljs-attr">input</span>: process.<span class="hljs-property">stdin</span>,
9391cb0ef41Sopenharmony_ci  <span class="hljs-attr">output</span>: process.<span class="hljs-property">stdout</span>,
9401cb0ef41Sopenharmony_ci});
9411cb0ef41Sopenharmony_ci
9421cb0ef41Sopenharmony_cinet.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> {
9431cb0ef41Sopenharmony_ci  connections += <span class="hljs-number">1</span>;
9441cb0ef41Sopenharmony_ci  repl.<span class="hljs-title function_">start</span>({
9451cb0ef41Sopenharmony_ci    <span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via Unix socket> '</span>,
9461cb0ef41Sopenharmony_ci    <span class="hljs-attr">input</span>: socket,
9471cb0ef41Sopenharmony_ci    <span class="hljs-attr">output</span>: socket,
9481cb0ef41Sopenharmony_ci  }).<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">() =></span> {
9491cb0ef41Sopenharmony_ci    socket.<span class="hljs-title function_">end</span>();
9501cb0ef41Sopenharmony_ci  });
9511cb0ef41Sopenharmony_ci}).<span class="hljs-title function_">listen</span>(<span class="hljs-string">'/tmp/node-repl-sock'</span>);
9521cb0ef41Sopenharmony_ci
9531cb0ef41Sopenharmony_cinet.<span class="hljs-title function_">createServer</span>(<span class="hljs-function">(<span class="hljs-params">socket</span>) =></span> {
9541cb0ef41Sopenharmony_ci  connections += <span class="hljs-number">1</span>;
9551cb0ef41Sopenharmony_ci  repl.<span class="hljs-title function_">start</span>({
9561cb0ef41Sopenharmony_ci    <span class="hljs-attr">prompt</span>: <span class="hljs-string">'Node.js via TCP socket> '</span>,
9571cb0ef41Sopenharmony_ci    <span class="hljs-attr">input</span>: socket,
9581cb0ef41Sopenharmony_ci    <span class="hljs-attr">output</span>: socket,
9591cb0ef41Sopenharmony_ci  }).<span class="hljs-title function_">on</span>(<span class="hljs-string">'exit'</span>, <span class="hljs-function">() =></span> {
9601cb0ef41Sopenharmony_ci    socket.<span class="hljs-title function_">end</span>();
9611cb0ef41Sopenharmony_ci  });
9621cb0ef41Sopenharmony_ci}).<span class="hljs-title function_">listen</span>(<span class="hljs-number">5001</span>);</code> <button class="copy-button">copy</button></pre>
9631cb0ef41Sopenharmony_ci<p>Running this application from the command line will start a REPL on stdin.
9641cb0ef41Sopenharmony_ciOther REPL clients may connect through the Unix socket or TCP socket. <code>telnet</code>,
9651cb0ef41Sopenharmony_cifor instance, is useful for connecting to TCP sockets, while <code>socat</code> can be used
9661cb0ef41Sopenharmony_cito connect to both Unix and TCP sockets.</p>
9671cb0ef41Sopenharmony_ci<p>By starting a REPL from a Unix socket-based server instead of stdin, it is
9681cb0ef41Sopenharmony_cipossible to connect to a long-running Node.js process without restarting it.</p>
9691cb0ef41Sopenharmony_ci<p>For an example of running a "full-featured" (<code>terminal</code>) REPL over
9701cb0ef41Sopenharmony_cia <code>net.Server</code> and <code>net.Socket</code> instance, see:
9711cb0ef41Sopenharmony_ci<a href="https://gist.github.com/TooTallNate/2209310">https://gist.github.com/TooTallNate/2209310</a>.</p>
9721cb0ef41Sopenharmony_ci<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:
9731cb0ef41Sopenharmony_ci<a href="https://gist.github.com/TooTallNate/2053342">https://gist.github.com/TooTallNate/2053342</a>.</p></section>
9741cb0ef41Sopenharmony_ci        <!-- API END -->
9751cb0ef41Sopenharmony_ci      </div>
9761cb0ef41Sopenharmony_ci    </div>
9771cb0ef41Sopenharmony_ci  </div>
9781cb0ef41Sopenharmony_ci</body>
9791cb0ef41Sopenharmony_ci</html>
980