This course has been updated! We now recommend you take the API Design in Node.js, v4 course.

Check out a free preview of the full API Design in Node.js (using Express & Mongo) course:
The "Executing Node" Lesson is part of the full, API Design in Node.js (using Express & Mongo) course featured in this preview video. Here's what you'd learn in this lesson:

JavaScript can be executed in Node directly from the command line or by passing the name of a JavaScript file as a command line argument. In either case, the JavaScript code will behave similarly to being run in a browser. Scott demonstrates both methods of execution.

Get Unlimited Access Now

Transcript from the "Executing Node" Lesson

[00:00:00]
>> [MUSIC]

[00:00:04]
>> Scott Moss: So let's do a little bit of digging around with Node. So, I just showed you how to run Node in the context of a your terminal. But you can also run Node against a file. So, I mean, who wants to write Node in here? Nobody wants to do that.

[00:00:19] You can't even test it. So you probably, you want to write some JavaScript to the file, and then execute it with Node. So the way you would do that is, and I'll just make a file, you don't have to follow along, you guys are gonna start digging in a little bit.

[00:00:34]
>> Scott Moss: So if we had a file, I was gonna call it test.js. And I do some, I'll just do a console.log here.
>> Scott Moss: To execute this file, all I have to do is go into the terminal, type in Node, and then the path to that file, which is test.

[00:00:50] I don't have to put test.js. I can just put test and it'll execute it. So there it is, the console.log, hey. It executed it, and then it exited it. All right? So, it's not running continuously. All right. So, anything that I do in here, it'll work. So, as you can see, the console just prints out to the terminal.

[00:01:09] Pretty much the same way it would in the window. There are some differences, but just to show you that this is real JavaScript. That's executing it. So that's how you would run Node against a file.
>> Scott Moss: Any questions on that?
>> Speaker 2: Can you make the text bigger, they're asking?

[00:01:30]
>> Scott Moss: Here, or the, here?
>> Speaker 2: In your editor. They're asking-
>> Scott Moss: My editor. Sorry. Yeah, I must have minimized it last night. Yeah. There we go.
>> Speaker 2: Did you get the question on the, he's seen some people use exports=module.exports.
>> Scott Moss: Yeah.
>> Speaker 2: What does that mean?
>> Scott Moss: Yeah,so, that's like a way to safeguard yourself from not doing a module.exports and then doing a exports somewhere else in the file.

[00:01:57] So by doing exports equals module.exports. That's, I'm just gonna keep everything safe. So I'm still only gonna be able to export one thing, and then I'm guaranteed that some time in the future I don't come down here and say exports.other equals a value. And then, that will never get exported because we had a module.exports.

[00:02:24] So it's just a way of keeping yourself safe. You don't have to do it. But there are a lot of patterns as far as exporting. I'll show you some of the other patterns. But that's a pretty standard pattern. You see that a lot.
>> Scott Moss: But it's just a way of making sure you don't try to export something later after you module.export.

[00:02:46] And not have it be able to be required in another file and have some unexpected side effects.