Use the Node.js REPL shell

Will Button
InstructorWill Button

Share this video with your friends

Send Tweet
Published 7 years ago
Updated 3 years ago

In this lesson, you will learn how to use the interactive Node.js REPL (Read - Evaluate - Print - Loop) shell. The REPL shell allows you to enter javascript directly into a shell prompt and have the results evaluated by the node.js engine immediately. This is extremely useful for testing, debugging, or experimenting with new features to understand how they work. Topics covered include creating variables, functions, the reserved underscore ( _ ) character, REPL commands as well as saving and loading your session to prevent losing work.

[00:01] In this lesson, we're going to be talking about the node REPL. REPL, R-E-P-L, actually stands for read, evaluate, print, loop. The easiest way to think about this is it's like a command line interface for node.

[00:16] One of the places it's really helpful to have this interface is when you're testing out new code, or want to isolate something and understand how it works better without having to go through the process of writing a bunch of code, and then, finding a server to deploy it to and all that kind of stuff.

[00:33] You want to execute it right inside of its native environment. You can do that by typing the node command without any additional arguments. It brings you to a prompt here. We can do anything that we could do inside of node here. We can do some math operations. We can assign some variables.

[00:55] One of the things you'll notice here is that we have this undefined getting returned when I assign a variable. Now, the reason that happens is because the result of that variable assignment returns nothing.

[01:11] It's therefore undefined. It's not actually an error or a problem with anything. We can validate that by checking to see that our variables exist and they do.

[01:22] The underscore character inside the REPL interface is actually reserved. It accesses the result of the last operation. If we do underscore plus four, you see we get 10, because the underscore was set to be the result of our last operation, which was 1+2+3, or six.

[01:44] We can also create some functions. You can execute those functions also taking full advantage of tab complete. I hit the tab there and it completed that. Tab complete works for local variables, as well as all global variables.

[02:05] You probably won't be working with this very long before you get really tired of trying to type your functions out on one line. That's not a problem, because you can do multi-line functions.

[02:18] What happens is when the REPL doesn't have enough input from you to do a complete operation, it returns the ellipsis or the three dots here to prompt you for more input. We can give it some more input. We're still not complete till we type our closing brace.

[02:43] Now, it's actually complete and we can execute that function. If you find yourself in a scenario where you've made a mistake and you need to abandon it, you can use control C and that will kick you out of the current context.

[02:59] You can also type the word .break or you can use .clear. .Break, .clear, and control C all execute the same operations, and that's just to cancel the current context and return you to the prompt. Another command that's handy is "save." You can use the .save followed by a file name.

[03:29] Now, our entire session is saved to that file. If we were to exit node by typing either control D, or the .exit command whenever we return later, we can see that same, our function we created earlier, save my name is no longer there.

[03:49] If we do a .load and provide the session file that we created earlier, our function has been returned, as well as all of the rest of our session state from the previous session.

[04:04] One last command that may be of interest to you is the .help command and that will show you all of the REPL commands that are available to you here that we've covered, as well as a little explanation of how those commands work.

David Choi
David Choi
~ 4 years ago

How can we tell when a video was first released?

~ 4 years ago

how do I donwload the code