Introduction to Node.js Running Node.js Code
This course has been updated! We now recommend you take the Introduction to Node.js, v2 course.
Transcript from the "Running Node.js Code" Lesson
[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: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.