/usr/share/doc/nodejs/api/repl.html is in nodejs 4.2.6~dfsg-1ubuntu4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 | <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>REPL Node.js v4.2.6 Manual & Documentation</title>
<link rel="stylesheet" href="assets/style.css">
<link rel="stylesheet" href="assets/sh.css">
<link rel="canonical" href="repl.html">
</head>
<body class="alt apidoc" id="api-section-repl">
<div id="content" class="clearfix">
<div id="column2" class="interior">
<div id="intro" class="interior">
<a href="/" title="Go back to the home page">
Node.js (1)
</a>
</div>
<ul>
<li><a class="nav-documentation" href="documentation.html">About these Docs</a></li>
<li><a class="nav-synopsis" href="synopsis.html">Synopsis</a></li>
<li><a class="nav-assert" href="assert.html">Assertion Testing</a></li>
<li><a class="nav-buffer" href="buffer.html">Buffer</a></li>
<li><a class="nav-addons" href="addons.html">C/C++ Addons</a></li>
<li><a class="nav-child_process" href="child_process.html">Child Processes</a></li>
<li><a class="nav-cluster" href="cluster.html">Cluster</a></li>
<li><a class="nav-console" href="console.html">Console</a></li>
<li><a class="nav-crypto" href="crypto.html">Crypto</a></li>
<li><a class="nav-debugger" href="debugger.html">Debugger</a></li>
<li><a class="nav-dns" href="dns.html">DNS</a></li>
<li><a class="nav-domain" href="domain.html">Domain</a></li>
<li><a class="nav-errors" href="errors.html">Errors</a></li>
<li><a class="nav-events" href="events.html">Events</a></li>
<li><a class="nav-fs" href="fs.html">File System</a></li>
<li><a class="nav-globals" href="globals.html">Globals</a></li>
<li><a class="nav-http" href="http.html">HTTP</a></li>
<li><a class="nav-https" href="https.html">HTTPS</a></li>
<li><a class="nav-modules" href="modules.html">Modules</a></li>
<li><a class="nav-net" href="net.html">Net</a></li>
<li><a class="nav-os" href="os.html">OS</a></li>
<li><a class="nav-path" href="path.html">Path</a></li>
<li><a class="nav-process" href="process.html">Process</a></li>
<li><a class="nav-punycode" href="punycode.html">Punycode</a></li>
<li><a class="nav-querystring" href="querystring.html">Query Strings</a></li>
<li><a class="nav-readline" href="readline.html">Readline</a></li>
<li><a class="nav-repl active" href="repl.html">REPL</a></li>
<li><a class="nav-stream" href="stream.html">Stream</a></li>
<li><a class="nav-string_decoder" href="string_decoder.html">String Decoder</a></li>
<li><a class="nav-timers" href="timers.html">Timers</a></li>
<li><a class="nav-tls" href="tls.html">TLS/SSL</a></li>
<li><a class="nav-tty" href="tty.html">TTY</a></li>
<li><a class="nav-dgram" href="dgram.html">UDP/Datagram</a></li>
<li><a class="nav-url" href="url.html">URL</a></li>
<li><a class="nav-util" href="util.html">Utilities</a></li>
<li><a class="nav-v8" href="v8.html">V8</a></li>
<li><a class="nav-vm" href="vm.html">VM</a></li>
<li><a class="nav-zlib" href="zlib.html">ZLIB</a></li>
</ul>
</div>
<div id="column1" data-id="repl" class="interior">
<header>
<h1>Node.js v4.2.6 Documentation</h1>
<div id="gtoc">
<p>
<a href="index.html" name="toc">Index</a> |
<a href="all.html">View on single page</a> |
<a href="repl.json">View as JSON</a>
</p>
</div>
<hr>
</header>
<div id="toc">
<h2>Table of Contents</h2>
<ul>
<li><a href="#repl_repl">REPL</a><ul>
<li><a href="#repl_environment_variable_options">Environment Variable Options</a></li>
<li><a href="#repl_persistent_history">Persistent History</a><ul>
<li><a href="#repl_node_repl_history_file">NODE_REPL_HISTORY_FILE</a></li>
</ul>
</li>
<li><a href="#repl_repl_features">REPL Features</a><ul>
<li><a href="#repl_customizing_object_displays_in_the_repl">Customizing Object displays in the REPL</a></li>
</ul>
</li>
<li><a href="#repl_class_replserver">Class: REPLServer</a><ul>
<li><a href="#repl_event_exit">Event: 'exit'</a></li>
<li><a href="#repl_event_reset">Event: 'reset'</a></li>
<li><a href="#repl_replserver_definecommand_keyword_cmd">replServer.defineCommand(keyword, cmd)</a></li>
<li><a href="#repl_replserver_displayprompt_preservecursor">replServer.displayPrompt([preserveCursor])</a></li>
</ul>
</li>
<li><a href="#repl_repl_start_options">repl.start(options)</a></li>
</ul>
</li>
</ul>
</div>
<div id="apicontent">
<h1>REPL<span><a class="mark" href="#repl_repl" id="repl_repl">#</a></span></h1>
<pre class="api_stability_2">Stability: 2 - Stable</pre><p>A Read-Eval-Print-Loop (REPL) is available both as a standalone program and
easily includable in other programs. The REPL provides a way to interactively
run JavaScript and see the results. It can be used for debugging, testing, or
just trying things out.
</p>
<p>By executing <code>node</code> without any arguments from the command-line you will be
dropped into the REPL. It has simplistic emacs line-editing.
</p>
<pre><code>mjr:~$ node
Type '.help' for options.
> a = [ 1, 2, 3];
[ 1, 2, 3 ]
> a.forEach(function (v) {
... console.log(v);
... });
1
2
3</code></pre>
<p>For advanced line-editors, start Node.js with the environmental variable
<code>NODE_NO_READLINE=1</code>. This will start the main and debugger REPL in canonical
terminal settings which will allow you to use with <code>rlwrap</code>.
</p>
<p>For example, you could add this to your bashrc file:
</p>
<pre><code>alias node="env NODE_NO_READLINE=1 rlwrap node"</code></pre>
<h2>Environment Variable Options<span><a class="mark" href="#repl_environment_variable_options" id="repl_environment_variable_options">#</a></span></h2>
<p>The built-in repl (invoked by running <code>node</code> or <code>node -i</code>) may be controlled
via the following environment variables:
</p>
<ul>
<li><code>NODE_REPL_HISTORY</code> - When a valid path is given, persistent REPL history
will be saved to the specified file rather than <code>.node_repl_history</code> in the
user's home directory. Setting this value to <code>""</code> will disable persistent
REPL history.</li>
<li><code>NODE_REPL_HISTORY_SIZE</code> - defaults to <code>1000</code>. Controls how many lines of
history will be persisted if history is available. Must be a positive number.</li>
<li><code>NODE_REPL_MODE</code> - may be any of <code>sloppy</code>, <code>strict</code>, or <code>magic</code>. Defaults
to <code>magic</code>, which will automatically run "strict mode only" statements in
strict mode.</li>
</ul>
<h2>Persistent History<span><a class="mark" href="#repl_persistent_history" id="repl_persistent_history">#</a></span></h2>
<p>By default, the REPL will persist history between <code>node</code> REPL sessions by saving
to a <code>.node_repl_history</code> file in the user's home directory. This can be
disabled by setting the environment variable <code>NODE_REPL_HISTORY=""</code>.
</p>
<h3>NODE_REPL_HISTORY_FILE<span><a class="mark" href="#repl_node_repl_history_file" id="repl_node_repl_history_file">#</a></span></h3>
<pre class="api_stability_0">Stability: 0 - Deprecated: Use <code>NODE_REPL_HISTORY</code> instead.</pre><p>Previously in Node.js/io.js v2.x, REPL history was controlled by using a
<code>NODE_REPL_HISTORY_FILE</code> environment variable, and the history was saved in JSON
format. This variable has now been deprecated, and your REPL history will
automatically be converted to using plain text. The new file will be saved to
either your home directory, or a directory defined by the <code>NODE_REPL_HISTORY</code>
variable, as documented below.
</p>
<h2>REPL Features<span><a class="mark" href="#repl_repl_features" id="repl_repl_features">#</a></span></h2>
<!-- type=misc -->
<p>Inside the REPL, Control+D will exit. Multi-line expressions can be input.
Tab completion is supported for both global and local variables.
</p>
<p>Core modules will be loaded on-demand into the environment. For example,
accessing <code>fs</code> will <code>require()</code> the <code>fs</code> module as <code>global.fs</code>.
</p>
<p>The special variable <code>_</code> (underscore) contains the result of the last expression.
</p>
<pre><code>> [ 'a', 'b', 'c' ]
[ 'a', 'b', 'c' ]
> _.length
3
> _ += 1
4</code></pre>
<p>The REPL provides access to any variables in the global scope. You can expose
a variable to the REPL explicitly by assigning it to the <code>context</code> object
associated with each <code>REPLServer</code>. For example:
</p>
<pre><code>// repl_test.js
const repl = require('repl');
var msg = 'message';
repl.start('> ').context.m = msg;</code></pre>
<p>Things in the <code>context</code> object appear as local within the REPL:
</p>
<pre><code>mjr:~$ node repl_test.js
> m
'message'</code></pre>
<p>There are a few special REPL commands:
</p>
<ul>
<li><code>.break</code> - While inputting a multi-line expression, sometimes you get lost
or just don't care about completing it. <code>.break</code> will start over.</li>
<li><code>.clear</code> - Resets the <code>context</code> object to an empty object and clears any
multi-line expression.</li>
<li><code>.exit</code> - Close the I/O stream, which will cause the REPL to exit.</li>
<li><code>.help</code> - Show this list of special commands.</li>
<li><code>.save</code> - Save the current REPL session to a file<blockquote>
<p>.save ./file/to/save.js</p>
</blockquote>
</li>
<li><code>.load</code> - Load a file into the current REPL session.<blockquote>
<p>.load ./file/to/load.js</p>
</blockquote>
</li>
</ul>
<p>The following key combinations in the REPL have these special effects:
</p>
<ul>
<li><code><ctrl>C</code> - Similar to the <code>.break</code> keyword. Terminates the current
command. Press twice on a blank line to forcibly exit.</li>
<li><code><ctrl>D</code> - Similar to the <code>.exit</code> keyword.</li>
<li><code><tab></code> - Show both global and local(scope) variables</li>
</ul>
<h3>Customizing Object displays in the REPL<span><a class="mark" href="#repl_customizing_object_displays_in_the_repl" id="repl_customizing_object_displays_in_the_repl">#</a></span></h3>
<p>The REPL module internally uses
<a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a>, when printing values. However, <code>util.inspect</code> delegates the
call to the object's <code>inspect()</code> function, if it has one. You can read more
about this delegation <a href="util.html#util_custom_inspect_function_on_objects">here</a>.
</p>
<p>For example, if you have defined an <code>inspect()</code> function on an object, like this:
</p>
<pre><code>> var obj = { foo: 'this will not show up in the inspect() output' };
undefined
> obj.inspect = function() {
... return { bar: 'baz' };
... };
[Function]</code></pre>
<p>and try to print <code>obj</code> in REPL, it will invoke the custom <code>inspect()</code> function:
</p>
<pre><code>> obj
{ bar: 'baz' }</code></pre>
<h2>Class: REPLServer<span><a class="mark" href="#repl_class_replserver" id="repl_class_replserver">#</a></span></h2>
<p>This inherits from <a href="readline.html#readline_class_interface">Readline Interface</a> with the following events:
</p>
<h3>Event: 'exit'<span><a class="mark" href="#repl_event_exit" id="repl_event_exit">#</a></span></h3>
<p><code>function () {}</code>
</p>
<p>Emitted when the user exits the REPL in any of the defined ways. Namely, typing
<code>.exit</code> at the repl, pressing Ctrl+C twice to signal <code>SIGINT</code>, or pressing Ctrl+D
to signal <code>'end'</code> on the <code>input</code> stream.
</p>
<p>Example of listening for <code>exit</code>:
</p>
<pre><code>replServer.on('exit', () => {
console.log('Got "exit" event from repl!');
process.exit();
});</code></pre>
<h3>Event: 'reset'<span><a class="mark" href="#repl_event_reset" id="repl_event_reset">#</a></span></h3>
<p><code>function (context) {}</code>
</p>
<p>Emitted when the REPL's context is reset. This happens when you type <code>.clear</code>.
If you start the repl with <code>{ useGlobal: true }</code> then this event will never
be emitted.
</p>
<p>Example of listening for <code>reset</code>:
</p>
<pre><code>// Extend the initial repl context.
var replServer = repl.start({ options ... });
someExtension.extend(r.context);
// When a new context is created extend it as well.
replServer.on('reset', (context) => {
console.log('repl has a new context');
someExtension.extend(context);
});</code></pre>
<h3>replServer.defineCommand(keyword, cmd)<span><a class="mark" href="#repl_replserver_definecommand_keyword_cmd" id="repl_replserver_definecommand_keyword_cmd">#</a></span></h3>
<div class="signature"><ul>
<li><code>keyword</code> <span class="type">String</span></li>
<li><code>cmd</code> <span class="type">Object|Function</span></li>
</div></ul>
<p>Makes a command available in the REPL. The command is invoked by typing a <code>.</code>
followed by the keyword. The <code>cmd</code> is an object with the following values:
</p>
<ul>
<li><code>help</code> - help text to be displayed when <code>.help</code> is entered (Optional).</li>
<li><code>action</code> - a function to execute, potentially taking in a string argument,
when the command is invoked, bound to the REPLServer instance (Required).</li>
</ul>
<p>If a function is provided instead of an object for <code>cmd</code>, it is treated as the
<code>action</code>.
</p>
<p>Example of defining a command:
</p>
<pre><code>// repl_test.js
const repl = require('repl');
var replServer = repl.start();
replServer.defineCommand('sayhello', {
help: 'Say hello',
action: function(name) {
this.write(`Hello, ${name}!\n');
this.displayPrompt();
}
});</code></pre>
<p>Example of invoking that command from the REPL:
</p>
<pre><code>> .sayhello Node.js User
Hello, Node.js User!</code></pre>
<h3>replServer.displayPrompt([preserveCursor])<span><a class="mark" href="#repl_replserver_displayprompt_preservecursor" id="repl_replserver_displayprompt_preservecursor">#</a></span></h3>
<div class="signature"><ul>
<li><code>preserveCursor</code> <span class="type">Boolean</span></li>
</div></ul>
<p>Like <a href="readline.html#readline_rl_prompt_preservecursor"><code>readline.prompt</code></a> except also adding indents with ellipses when inside
blocks. The <code>preserveCursor</code> argument is passed to <a href="readline.html#readline_rl_prompt_preservecursor"><code>readline.prompt</code></a>. This is
used primarily with <code>defineCommand</code>. It's also used internally to render each
prompt line.
</p>
<h2>repl.start(options)<span><a class="mark" href="#repl_repl_start_options" id="repl_repl_start_options">#</a></span></h2>
<p>Returns and starts a <code>REPLServer</code> instance, that inherits from
<a href="readline.html#readline_class_interface">Readline Interface</a>. Accepts an "options" Object that takes
the following values:
</p>
<ul>
<li><p><code>prompt</code> - the prompt and <code>stream</code> for all I/O. Defaults to <code>> </code>.</p>
</li>
<li><p><code>input</code> - the readable stream to listen to. Defaults to <code>process.stdin</code>.</p>
</li>
<li><p><code>output</code> - the writable stream to write readline data to. Defaults to
<code>process.stdout</code>.</p>
</li>
<li><p><code>terminal</code> - pass <code>true</code> if the <code>stream</code> should be treated like a TTY, and
have ANSI/VT100 escape codes written to it. Defaults to checking <code>isTTY</code>
on the <code>output</code> stream upon instantiation.</p>
</li>
<li><p><code>eval</code> - function that will be used to eval each given line. Defaults to
an async wrapper for <code>eval()</code>. See below for an example of a custom <code>eval</code>.</p>
</li>
<li><p><code>useColors</code> - a boolean which specifies whether or not the <code>writer</code> function
should output colors. If a different <code>writer</code> function is set then this does
nothing. Defaults to the repl's <code>terminal</code> value.</p>
</li>
<li><p><code>useGlobal</code> - if set to <code>true</code>, then the repl will use the <code>global</code> object,
instead of running scripts in a separate context. Defaults to <code>false</code>.</p>
</li>
<li><p><code>ignoreUndefined</code> - if set to <code>true</code>, then the repl will not output the
return value of command if it's <code>undefined</code>. Defaults to <code>false</code>.</p>
</li>
<li><p><code>writer</code> - the function to invoke for each command that gets evaluated which
returns the formatting (including coloring) to display. Defaults to
<code>util.inspect</code>.</p>
</li>
<li><p><code>replMode</code> - controls whether the repl runs all commands in strict mode,
default mode, or a hybrid mode ("magic" mode.) Acceptable values are:</p>
<ul>
<li><code>repl.REPL_MODE_SLOPPY</code> - run commands in sloppy mode.</li>
<li><code>repl.REPL_MODE_STRICT</code> - run commands in strict mode. This is equivalent to
prefacing every repl statement with <code>'use strict'</code>.</li>
<li><code>repl.REPL_MODE_MAGIC</code> - attempt to run commands in default mode. If they
fail to parse, re-try in strict mode.</li>
</ul>
</li>
</ul>
<p>You can use your own <code>eval</code> function if it has following signature:
</p>
<pre><code>function eval(cmd, context, filename, callback) {
callback(null, result);
}</code></pre>
<p>On tab completion - <code>eval</code> will be called with <code>.scope</code> as an input string. It
is expected to return an array of scope names to be used for the auto-completion.
</p>
<p>Multiple REPLs may be started against the same running instance of Node.js. Each
will share the same global object but will have unique I/O.
</p>
<p>Here is an example that starts a REPL on stdin, a Unix socket, and a TCP socket:
</p>
<pre><code>const net = require('net');
const repl = require('repl');
var connections = 0;
repl.start({
prompt: 'Node.js via stdin> ',
input: process.stdin,
output: process.stdout
});
net.createServer((socket) => {
connections += 1;
repl.start({
prompt: 'Node.js via Unix socket> ',
input: socket,
output: socket
}).on('exit', () => {
socket.end();
})
}).listen('/tmp/node-repl-sock');
net.createServer((socket) => {
connections += 1;
repl.start({
prompt: 'Node.js via TCP socket> ',
input: socket,
output: socket
}).on('exit', () => {
socket.end();
});
}).listen(5001);</code></pre>
<p>Running this program from the command line will start a REPL on stdin. Other
REPL clients may connect through the Unix socket or TCP socket. <code>telnet</code> is useful
for connecting to TCP sockets, and <code>socat</code> can be used to connect to both Unix and
TCP sockets.
</p>
<p>By starting a REPL from a Unix socket-based server instead of stdin, you can
connect to a long-running Node.js process without restarting it.
</p>
<p>For an example of running a "full-featured" (<code>terminal</code>) REPL over
a <code>net.Server</code> and <code>net.Socket</code> instance, see: <a href="https://gist.github.com/2209310">https://gist.github.com/2209310</a>
</p>
<p>For an example of running a REPL instance over <code>curl(1)</code>,
see: <a href="https://gist.github.com/2053342">https://gist.github.com/2053342</a>
</p>
</div>
</div>
</div>
<script src="assets/sh_main.js"></script>
<script src="assets/sh_javascript.min.js"></script>
<script>highlight(undefined, undefined, 'pre');</script>
</body>
</html>
|