Sanket Meghani

@sanketmeghani

Know Your Node REPL

Node comes with a built in module for REPL implementation. REPL stands for Read-Evaluate-Print-Loop. It is very handy when we want to quickly test a JavaScript code snippet. Node REPL is available both as a standalone program as well as includible in other applications. In this article, we will go through some of the features of Node REPL which could be very useful in day to day to development.

Starting REPL

We can start Node REPL by typing node in the command prompt or shell after installing node. Once we are into REPL, we can execute any JavaScript code and it will read the code, evaluate the expressions and print the output as shown below.

Autocomplete — Tab & Tab Tab

Node REPL supports autocomplete using a tab key. At any point, we can press tab key to autocomplete partially typed command or expression. If there are multiple commands/expressions starting with partially typed text, then pressing tab second time (tab followed by tab) will show a list of possible commands with the typed prefix.

For example, typing ‘s’ and pressing tab key will show all possible valid expressions starting with ‘s’.

Try pressing tab followed by tab without entering anything and we can see all the modules exported by Node by default.

We can also see a list of apis/functions/properties available within a particular module/object by pressing the module/object name followed by a ‘.’ and pressing tab two times. For example, to see a list of functions/properties of Array, type Array. and press tab two times. We see a list of members exposed by Array.

Try pressing global. and tab. This will list all the modules and objects available to all the node scripts without explicitly importing/requiring them in the file.

Underscore

Underscore(_) is a special variable in node which stores the result of last expression evaluation. It can be used to access result of last command execution — similar to $? in bash.

Special Dot Commands

Node REPL supports special commands which starts with a dot ‘.’. Type a . and press double tab to see a list of dot commands. Alternatively, type .help to see all dot commands with their description.

.load

Load command can be used to load a JavaScript file into current REPL session. After the file is loaded, all the functions, variables defined in the file would be available in current REPL session. For example, if we have a file named math.js

Then we can load it in current REPL session using .load as below.

.break & .clear

Break & Clear command can be used to terminate & come out of a multi line session. Sometimes while copy pasting code snippet into REPL, we get stuck. We can type .break to terminate a multi-line session in such cases and get back to REPL prompt.

Clear is just an alias for break command.

.editor

Editor command will get us into editor mode. Editor mode is a convenient alternative to creating and loading a JavaScript file into current session. It allows us to write multiple lines code quite conveniently compare to default multi-line mode.

Press ^D to finish multi-line editing and ^C to cancel editing.

.save

Save command would save entire session history to a file. This could be useful after a long REPL session.

.exit

Finally, .exit command will terminate the REPL session. By default to exit the current session, we need to press ^C twice. By typing .exit will directly exit the session.

Post Script

Node REPL is very convenient and powerful tool during development. We have list down only a few features. It also allows us to create custom REPL sessions by requiring ‘repl’ module and invoking repl.start(). Refer Node REPL documentation for full list of features and possible customization.

More by Sanket Meghani

Topics of interest

More Related Stories