paint-brush
Know Your Node REPLby@sanketmeghani
8,742 reads
8,742 reads

Know Your Node REPL

by Sanket Meghani
Sanket Meghani HackerNoon profile picture

Sanket Meghani

@sanketmeghani

Technical Advisor & Fullstack Architect

November 21st, 2017
Read on Terminal Reader
Read this story in a terminal
Print this story
Read this story w/o Javascript
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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.

Company Mentioned

Mention Thumbnail
Few
featured image - Know Your Node REPL
Sanket Meghani HackerNoon profile picture
Sanket Meghani

Sanket Meghani

@sanketmeghani

Technical Advisor & Fullstack Architect

image

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.

image

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’.

image

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

image

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.

image

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.

image

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.

image

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.

image

.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.

image

.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.

image

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.

image

.save

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

image

.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.

L O A D I N G
. . . comments & more!

About Author

Sanket Meghani HackerNoon profile picture
Sanket Meghani@sanketmeghani
Technical Advisor & Fullstack Architect

TOPICS

THIS ARTICLE WAS FEATURED IN...

Permanent on Arweave
Read on Terminal Reader
Read this story in a terminal
 Terminal
Read this story w/o Javascript
Read this story w/o Javascript
 Lite
Jorin
Travishorn
Nodeweekly
Github
Vercel
Github
X REMOVE AD