Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Run JavaScript in the Terminal with the Node REPL

    John LindquistJohn Lindquist

    With Node installed, you can begin running JavaScript directly in the terminal. The Node REPL allows you to execute any JavaScript expression to check the output. You can even install packages from npm and load you local JavaScript files to test how libraries and functions run.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    Open your Terminal and start the Node REPL with just typing Node. Anything you type in here, like two plus three, will be evaluated and print out. When you need to type multiple lines, switch over to editor mode. This will allow you to write as much stuff as you need.

    So just sort a list here and then invoke it with two one three. Then hit control D to execute that. You'll see it prints out the sorted list. Then after, you'll still have this my sort in your context. So you can type my sort three one two and use that function however you want.

    At any time, you can say save and a filename, hit enter. Then in your editor, you can open up that file, and it will have that entire session for you.

    Or the opposite of that is to write your own file. I'll just write a function called add which takes an X and a Y and adds them together. Then I can say load app.js, and you can see it loaded the code from there. Now I can try out my add function. So add three and six, and we'll get nine. At any time, just type exit or .exit or control C to exit out of it.

    You can also install Node packages. So we'll say install date functions. I find it's useful to require them in a script you're going to load. So I'll require date functions. I want to use a distance in words. Then I can fire up Node, tell it to load app.js. Now I can play with distance in words. We'll try today compared to, let's see, 2018, January 1st. You can see that's four months from now.

    This will work with any project, large or small, a single script or a large project, where you can load in data and functions and play with them interactively...