{
  "source": "doc/api/repl.markdown",
  "modules": [
    {
      "textRaw": "REPL",
      "name": "repl",
      "desc": "<p>A Read-Eval-Print-Loop (REPL) is available both as a standalone program and easily\nincludable in other programs.  REPL provides a way to interactively run\nJavaScript and see the results.  It can be used for debugging, testing, or\njust trying things out.\n\n</p>\n<p>By executing <code>node</code> without any arguments from the command-line you will be\ndropped into the REPL. It has simplistic emacs line-editing.\n\n</p>\n<pre><code>mjr:~$ node\nType &apos;.help&apos; for options.\n&gt; a = [ 1, 2, 3];\n[ 1, 2, 3 ]\n&gt; a.forEach(function (v) {\n...   console.log(v);\n...   });\n1\n2\n3</code></pre>\n<p>For advanced line-editors, start node with the environmental variable <code>NODE_NO_READLINE=1</code>.\nThis will start the REPL in canonical terminal settings which will allow you to use with <code>rlwrap</code>.\n\n</p>\n<p>For example, you could add this to your bashrc file:\n\n</p>\n<pre><code>alias node=&quot;env NODE_NO_READLINE=1 rlwrap node&quot;</code></pre>\n",
      "methods": [
        {
          "textRaw": "repl.start([prompt], [stream], [eval], [useGlobal], [ignoreUndefined])",
          "type": "method",
          "name": "start",
          "desc": "<p>Starts a REPL with <code>prompt</code> as the prompt and <code>stream</code> for all I/O.  <code>prompt</code>\nis optional and defaults to <code>&gt; </code>.  <code>stream</code> is optional and defaults to\n<code>process.stdin</code>. <code>eval</code> is optional too and defaults to async wrapper for\n<code>eval()</code>.\n\n</p>\n<p>If <code>useGlobal</code> is set to true, then the repl will use the global object,\ninstead of running scripts in a separate context. Defaults to <code>false</code>.\n\n</p>\n<p>If <code>ignoreUndefined</code> is set to true, then the repl will not output return value\nof command if it&apos;s <code>undefined</code>. Defaults to <code>false</code>.\n\n</p>\n<p>You can use your own <code>eval</code> function if it has following signature:\n\n</p>\n<pre><code>function eval(cmd, callback) {\n  callback(null, result);\n}</code></pre>\n<p>Multiple REPLs may be started against the same running instance of node.  Each\nwill share the same global object but will have unique I/O.\n\n</p>\n<p>Here is an example that starts a REPL on stdin, a Unix socket, and a TCP socket:\n\n</p>\n<pre><code>var net = require(&quot;net&quot;),\n    repl = require(&quot;repl&quot;);\n\nconnections = 0;\n\nrepl.start(&quot;node via stdin&gt; &quot;);\n\nnet.createServer(function (socket) {\n  connections += 1;\n  repl.start(&quot;node via Unix socket&gt; &quot;, socket);\n}).listen(&quot;/tmp/node-repl-sock&quot;);\n\nnet.createServer(function (socket) {\n  connections += 1;\n  repl.start(&quot;node via TCP socket&gt; &quot;, socket);\n}).listen(5001);</code></pre>\n<p>Running this program from the command line will start a REPL on stdin.  Other\nREPL clients may connect through the Unix socket or TCP socket. <code>telnet</code> is useful\nfor connecting to TCP sockets, and <code>socat</code> can be used to connect to both Unix and\nTCP sockets.\n\n</p>\n<p>By starting a REPL from a Unix socket-based server instead of stdin, you can\nconnect to a long-running node process without restarting it.\n\n\n</p>\n",
          "signatures": [
            {
              "params": [
                {
                  "name": "prompt",
                  "optional": true
                },
                {
                  "name": "stream",
                  "optional": true
                },
                {
                  "name": "eval",
                  "optional": true
                },
                {
                  "name": "useGlobal",
                  "optional": true
                },
                {
                  "name": "ignoreUndefined",
                  "optional": true
                }
              ]
            }
          ]
        }
      ],
      "miscs": [
        {
          "textRaw": "REPL Features",
          "name": "REPL Features",
          "type": "misc",
          "desc": "<p>Inside the REPL, Control+D will exit.  Multi-line expressions can be input.\nTab completion is supported for both global and local variables.\n\n</p>\n<p>The special variable <code>_</code> (underscore) contains the result of the last expression.\n\n</p>\n<pre><code>&gt; [ &quot;a&quot;, &quot;b&quot;, &quot;c&quot; ]\n[ &apos;a&apos;, &apos;b&apos;, &apos;c&apos; ]\n&gt; _.length\n3\n&gt; _ += 1\n4</code></pre>\n<p>The REPL provides access to any variables in the global scope. You can expose\na variable to the REPL explicitly by assigning it to the <code>context</code> object\nassociated with each <code>REPLServer</code>.  For example:\n\n</p>\n<pre><code>// repl_test.js\nvar repl = require(&quot;repl&quot;),\n    msg = &quot;message&quot;;\n\nrepl.start().context.m = msg;</code></pre>\n<p>Things in the <code>context</code> object appear as local within the REPL:\n\n</p>\n<pre><code>mjr:~$ node repl_test.js\n&gt; m\n&apos;message&apos;</code></pre>\n<p>There are a few special REPL commands:\n\n</p>\n<ul>\n<li><code>.break</code> - While inputting a multi-line expression, sometimes you get lost\nor just don&apos;t care about completing it. <code>.break</code> will start over.</li>\n<li><code>.clear</code> - Resets the <code>context</code> object to an empty object and clears any\nmulti-line expression.</li>\n<li><code>.exit</code> - Close the I/O stream, which will cause the REPL to exit.</li>\n<li><code>.help</code> - Show this list of special commands.</li>\n<li><code>.save</code> - Save the current REPL session to a file<blockquote>\n<p>.save ./file/to/save.js</p>\n</blockquote>\n</li>\n<li><code>.load</code> - Load a file into the current REPL session.<blockquote>\n<p>.load ./file/to/load.js</p>\n</blockquote>\n</li>\n</ul>\n<p>The following key combinations in the REPL have these special effects:\n\n</p>\n<ul>\n<li><code>&lt;ctrl&gt;C</code> - Similar to the <code>.break</code> keyword.  Terminates the current\ncommand.  Press twice on a blank line to forcibly exit.</li>\n<li><code>&lt;ctrl&gt;D</code> - Similar to the <code>.exit</code> keyword.</li>\n</ul>\n"
        }
      ],
      "type": "module",
      "displayName": "REPL"
    }
  ]
}
