03 - How to use the Node REPL to test JavaScript on the command line

The important bits

  1. Just like the browser devtools, node provides a REPL(Read-Eval-Print-Loop) you can run from the command line in your terminal.
  2. Open your terminal and type node to start the repl.
  3. You can enter and execute any JavaScript into that prompt >.
  4. The terminal will execute the JS you entered.
  5. The terminal will print the result, and you can log the output and log any errors.

Exercise - Now you try it

1. Open your terminal and type `node` to start the repl. 3. Enter the following and press the `Return` key: var test = 'it works'; 4. Enter `test` and press `Return` key. 5. You should see `it works`.

Transcript

0:00 In this episode we'll demonstrate how to use the node REPL, but first let's take a look at some of the files we'll be dealing with

0:09 one file here called Test Mod,

0:13 and it contains a function called My test, and then we export that, so it can be included from another module. Great, Let's dive into the REPL. First, we'll need to see if we have node installed on this computer.

0:27 I've got node Version 10.

0:30 If you don't have node installed, take a look at the shownotes were a link to another episode where I show how to install node. We enter the REPL. By typing node and hitting enter. The node REPL is basically like a little sandbox where we can try out JavaScript commands. We can try out any of the node APIs, but we don't have access to the browser APIs. That means that things like alert() will not work here. Because node is meant as a server side language. Which means we don't have access to browser-only APIs. Some of the APIs are both in the browser and in node, and most of the JavaScript that runs in the browser will run on node, but some of the APIs are different, so what can we do here we can do things like type fs for the fs module, which shows us what some of the methods are available. Sir Ignou, Fs dot, make directory, read, file, read, sank, refile, sink, etc, do the same with console. We've got console log console that time and time and let's try that one.

1:35 Then you have to name it

1:37 an undefined. Here. Just means that this command doesn't returning. If it's still work just fine

1:43 and will end this one and it's like eleven seconds elapsed between those two commands, so this can be very useful if you're trying to time your Javascript to see how much time elapses between. Maybe on a P. I call something like that. Now. What if you have a big note app? And you have one piece that is not working right, Let's say you're importing a file as part of a route, and there's a problem there, but you're not sure what the problem is the rebels. A great place to isolate us. You could import it. You can require it in, and you can run it in isolation and kind of poke and prod it and see what happens. So let's load that module that I showed at the beginning of this episode, so if the sign it a variable that we want a sign that object to, and really, you could choose any name here oldest do mine require, and the name of that file was test mod. Can leave out the Dutch, as extension cannot find the module test mod. If you don't include the relative path. It'll assume it's in the node modules folder, which we don't have here. So to dot slash, which tells us that it's in the same folder. There you go undefined. Which means we now have access to that. The mine is an object with a method attached of my test, called mind up my test, and that says it worked, which means that function, Iran, so in summary. The rebel is a great way for you to test small snippets of Javascript Pd. See what methods and functions are available in this version. So here I am running Node Version Ten. This is a great way to see what kind of functionality works. What doesn't for you to test out small snippets You can even pull in files as modules and try them out. That's it. Good luck.

END