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

Scott demonstrates how to execute JavaScript code in the Node REPL. A REPL is like the console in a browser because it executes code and displays the result. Since Node.js is an OS-level runtime, it will not understand any browser-specific JavaScript API's.

Get Unlimited Access Now

Transcript from the "Executing Node" Lesson

>> The first thing I wanna talk about is how we can actually execute a Node program, because there's several ways, but we're really only gonna talk about the important ones. So the first one is called the Node REPL. And REPL stands for read evaluate, print, loop. It's basically an interactive shell that's gonna be in the execution context of the runtime of your choice.

[00:00:25] In this case, it's gonna be Node. So we'll have an interactive shell inside of our terminal where we can just write some JavaScript in the Node runtime, very similar to where if you open up the console in a browser like this, and you were able to interact. This is a REPL, right?

[00:00:40] So I can come in here and I can do stuff like this, right? So we're gonna do the same thing, but outside of the browser using Node.js. And the way that you can do that is if you just go to your terminal Here we go, here is my terminal.

[00:00:58] I'm actually going to go over to my folder, Am I here? I'm already in here. Okay, so I'll make this bigger. And what you can do is, you can just run the Node. First of all, just to double check you have Node installed, I recommend running which Node, if you're on Mac.

[00:01:16] And it's gonna point to where you have Node installed. You can see that I used nwm, so it's installed in this nwm folder. If you installed it from the actual Node source, you might see something like user bin if you're on Mac, and you'll see it installed there.

[00:01:28] On Windows, I have no idea what it's gonna say when you installed it, but just make sure that you have it. You can also just type in, node version, and see what version of Node you have. Just to make sure it's there. If you're getting an error when you type in the Node command, then it means it's not installed properly.

[00:01:44] So go back and follow those instructions. So once you do have it installed, now we wanna execute on that REPL, we wanna turn it on, you can just type in, node, with no arguments. And you should see something like this, welcome to Node.js, followed by the version that you're on.

[00:01:57] And then you could do something I call type "help" for information. But now we're in this prompt here, and it's pretty much console just like I showed you in the browser. So if I wanted to make a variable like this, you can see I have it, then I could actually just type it in.

[00:02:14] And before I even hit Enter to execute this, it's giving me that auto-complete like, there's the value of that variable. So as you can see, this is a great place to prototype different things, try out different features in JavaScript. I like to use the REPL when I'm doing something quick.

[00:02:29] I've even gotten to the point where I'll use the REPL for a calculator versus opening up the calculator app on my computer, because it's just faster to go into terminal and type in, node and do math, than it is to like go on a calculator and click things.

[00:02:41] So I use it for a bunch of random stuff all the time. But you probably won't be using the Node REPL to build any applications as you can tell. I mean, this isn't source code. This code isn't being written to a file anywhere. You can check it in the Git or GitHub.

[00:02:56] So, yeah, you probably couldn't build an application in a REPL, although I'm sure if you were to go search the internet, someone's probably done this before. But that's probably not gonna be you, and I do not recommend trying to build an app in the REPL. It's just a playground to get some stuff going.

[00:03:12] And then to exit the REPL, at least on the Mac, I can hit Control C. There we go. Press it again and it will exit for you. I think you can also just type in exit, maybe, let's see if that works. Nope, exit does not work. That's some other REPL.

[00:03:29] I get all these REPLs mixed up. So yeah, I just hit control C to exit that, and that way, I'm not inside of that REPL anymore, and now I'm back inside of my terminal environment here. So, like I said, you don't wanna be running a program inside the Node REPL for all the reasons that I stated.

[00:03:48] So the real way you wanna do it is, you wanna put your code in a file, just like you've done your whole life, your whole coding career, and we wanna execute that file against the Node runtime. And this is where it really gets interesting, because you're gonna find out that your VS code or whatever text editor you're using doesn't really know what context your JavaScript file is gonna be executed in.

[00:04:10] Is it gonna be executed in the browser? Is it gonna be executed on Node? Maybe it's executed on a mobile device, because JavaScript is basically universal, it can be executed in many different runtimes. So this is where it's very helpful for you to understand when your JavaScript code is gonna be executed and what environment it's gonna be in, because that's gonna determine what features you have access to, and what you're able to do inside of that code.

[00:04:38] So let's start with actually creating a file. And then let's try to execute that file with the Node runtime. So just make a new folder on your computer. I already have one here. It's just a blank folder. I'm gonna open it in VS code. There's nothing in it.

[00:04:53] And I am actually just gonna make a file here inside of this folder. I'm gonna call it index.js. And then what I'm gonna do is, let's try to do something we would normally do in the browser. I'm actually just gonna do an alert here. And I'm gonna say, hello, right?

[00:05:10] This will totally work in the browser. We know if we went to the browser right now, and we typed in, alert, this definitely works. Right? And like I said, Node.js is JavaScript. So if alert works in the browser, and that's JavaScript, then alert should also work in Node.js, because that too is JavaScript.

[00:05:31] So let's give it a try. So I'm gonna go back to my editor, type in, alert, go back to my terminal. And then to execute this file in the Node.js runtime, I just type in, node, just like we did with the REPL, followed by the path to that file.

[00:05:46] So I'm already inside of my files directory here, so it's gonna be relative and this is gonna be index.js. And then I can type in, enter. And immediately, I get an error here. So you can see that I get this thing called, ReferenceError: alert is not defined. And these are the differences that I'm talking about.

[00:06:03] This is why it's very important to understand where you're gonna be executing your JavaScript code because the same JavaScript could work differently depending on the environment that you're in. I could take this same file and add it to a script tag in a browser app and it would work totally fine.

[00:06:19] You probably shouldn't have an alert on your site, unless you want people to go running away screaming because it looks like spam, but it will work, right? But it's not gonna work here in Node, because there is no alert. Node doesn't have a user interface. There's something visual to Node.

[00:06:34] It's running on your operating system in the background. There's no CSS. There's no DOM. There is nothing visual about Node.js. It's a OS level language. So we don't have access to something like alert. And even if we did, what would you expect it to do? Would you expect to see a notification pop-up on your computer?

[00:06:53] Because actually, that would be pretty dope and someone should make that, if they haven't already. But no, it doesn't do that. So, these are some of the differences that we're going to discover. So let's actually do something that we can do in Node that does also work in the browser.

[00:07:06] And it's gonna be our favorite friend, console.log, right? I can't live without console.log. So I don't know how much y'all use console.log, but I cannot live without it. It's basically how I debug everything. I have solved many crises with console.log. So let's try to see if it works here in Node.js.

[00:07:30] And you can see, we do get that message, hello. So the console.log works just fine here. So this is where it gets tricky, it's like, well, what works in the browser and what doesn't work in the browser? What works in Node and what doesn't work in Node? The answer is, it depends.

[00:07:44] If it's usually something with UI facing, anything with the DOM, anything like that, like querying a DOM elements, things like that, it's not gonna work in Node because there's nothing there. And then on the flip side, if there's usually something to do with file system or any OS level things like networking, it's probably not gonna work in the browser.

[00:08:02] So I can tell you right now, I don't think anyone remembers all the different things that work. It's more about just, you do this enough, you do a lot of trial and error, you understand what each environment has access to, and you can figure it out from there.

[00:08:15] Besides, the computer will tell you when you're wrong, so you don't have to guess. It'll literally just, as you saw, will tell you if this doesn't work here and that's when you'll know