This course has been updated! We now recommend you take the Introduction to Node.js, v2 course.

Check out a free preview of the full Introduction to Node.js course:
The "Running Node.js Code" Lesson is part of the full, Introduction to Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Scott demonstrates how to run a JavaScript program using the Node.js CLI executable or REPL.

Get Unlimited Access Now

Transcript from the "Running Node.js Code" Lesson

>> Scott Moss: Cool, so now we're gonna get into running Node. Okay, so once you get Node installed, again, we're gonna install it after we go through this and do the exercise. But once you have it, how do you use it? What do you do with it? Because you're so used to writing JavaScript in the browser, and the browser just evaluates it for you.

[00:00:16] So how do you get Node to evaluate your JavaScript and what are the differences? What are the different types of evaluations? How does that work? There's really only two ways to evaluate your code and Node. There's an interactive REPL and then there's a CLI executable, right? Basically the REPL is for playing around and the CLI's for everything else.

[00:00:38] That's it, you are never gonna use the REPL for anything else. REPL basically stands for Read, Evaluate, Print, Loop. This is like an interactive environment that you can just write code, that is ephemeral, like it's gonna disappear, it's not saved anywhere, it's not saved to a disk. You're just writing it in line and then evaluate.

[00:00:57] So that's why you wouldn't use it for anything else, cuz it's not written to a file, it's just written in time and then it's gone. So it's just for testing things out, you could think of it like, if you go to the console in the browser, that's what the REPL is.

[00:01:09] But in your terminal, it's the same thing, that's literally what it is, but with just better control. Sometimes in the console, you'll hit enter to go to the next line, but it actually evaluates, and you're, forgot to hit Shift Enter to go to the next line. It doesn't do all that wonky stuff.

[00:01:22] So interactive REPL, which is just for testing things out, like I said, it's like the console in the browser. And the way you do this is, once you have Node installed, you just type the word Node. That's it, just go in your terminal or whatever and just type in Node.

[00:01:36] No arguments or nothing, just type in Node. And then it'll be interactive and you can start writing JavaScripts. So to show you what that looks like, we're gonna do some of that here. I got so much stuff. Let me go to the other one. There we go. So like I said, you go to your terminal, you just type in Node, and you hit Enter.

[00:02:02] And then you get this little carrot thing here. That means you're in the REPL now. You're in the console. So now you can do JavaScript stuff in here, right, so,
>> Scott Moss: Now I'll do a JavaScript. See, it's just interactive. Pretty simple. Now, you can see again why you just wouldn't use this to write application.

[00:02:21] You couldn't write an application in the REPL. I hope nobody is doing this, and if they are, I wanna know how. I don't know how. Cool, so let me get rid of that. A lot of crazy stuff there. There we go. So yeah, you just type the Node command, no args, and then you begin.

[00:02:40] And then this CLI executable, this is how you execute Node apps that you've written. So if you've written an app in your text editor, which you will most likely be doing, how do you get that to evaluate. Well, now all you have to do is just use the same Node command.

[00:02:56] Except for you would pass in an argument to your file. All right, so it would be Node and then the path to your file .js. And that's it. That's the only argument you would pass. Same Node command within the path to the file that you wanna run. And you might be thinking, so does my entire Node app need to be in one file?

[00:03:17] Yes and no. [LAUGH] That would suck, to build and hold app in one file. Luckily for you, you don't have to worry about that, it's very similar to how script tags work, right? If the script tags are loaded in a certain order, the script tags underneath it have access to the ones before it.

[00:03:36] So, technically, yes, that last script file all that stuff is global has access to everything so it is in like one file, but they are all in separate files. So, we are going to talk about modules and stuff later, but no, you don't need to put everything in one file.

[00:03:48] But yes, Node will look at your dependencies and traverse them like a tree and figure out what to do so you don't have to do it. And again, we'll talk about that later. So again, to get out of here on the Mac is just Ctrl+C, and you can see it'll tell you to exit, press that twice.

[00:04:05] Or you can just type in .exit if you can't get out, so you don't get trapped in there. And then running a command, it's just Node, and then, like I said, a path to a file. I don't know what files I have on here, if any of them work.

[00:04:16] Where you can see, you can just type in a file here. Let me see, browser. This one's gonna break on purpose so let's check it out. Yeah, so there you go. You can see I tried to evaluate something. This one broke. This is a error. This is the same thing you would do on a console.

[00:04:33] So if you did a console, you tried to evaluate some jobs when you see error. So this is the error being printed out. But you can see node tried to go execute this file at this path right here. It did execute it, and then it ran into an error, so that's what happened.