Check out a free preview of the full Vanilla JavaScript Projects course:
The "Running Node Scripts" Lesson is part of the full, Vanilla JavaScript Projects course featured in this preview video. Here's what you'd learn in this lesson:

Anjana demonstrates how to run JavaScript in a JS file by creating a file and executing it with the `node` command. Differences between browser and Node JavaScript APIs are also discussed in this lesson.

Get Unlimited Access Now

Transcript from the "Running Node Scripts" Lesson

>> So the thing that we're mostly gonna be doing is writing some JavaScript code in a file, often with a .js extension, usually. And then running that script, as it were, in Node by passing the file name in. So, if folks want to take this opportunity to make a new file, whereAmi.js or whatever you want to call it.

[00:00:30] And you can do this in your code editor of choice, you can do it at the command line, if you would like, with. Your command line code editor of choice, I feel like people can. Okay, so if we create a script like whereAmI.js, which I'm just gonna do right in here by touching a file called whereAmI.js.

[00:00:59] And I'm just gonna open my little friend vim [LAUGH] because I don't want to just leave my window. So just real quickly, I'm gonna paste this in, which I can do now, and just a little Ctrl+V, okay. And then I'm going to quit vim [LAUGH] and let's just make sure with cat, which is just gonna print out whatever is in this file.

[00:01:22] Let's just make sure that this worked. And so, okay, so I have exactly whatever I had in my slides. Help me, [LAUGH] in this file now, okay, cool. So now we're gonna talk about what is all going on in there in a second. But we see some words that we recognize and we see some words that we don't recognize maybe.

[00:01:42] And for now we're just going to try it out, we're just going to node whereAmI.js. And my shell knows what files I have, so it's completing things for me, thank you, computer. So if I run this, it said, you are here, and then after a minute, it told me the path to my current working directory.

[00:02:08] And so let us look at the contents here, with some syntax highlighting, to see exactly how we got that information. And again, keeping in mind that there was a little delay, roughly a second or so between the first line printing and the second line. And so let's look at what's going on.

[00:02:28] We've got our regular old console.log, looks the same as our client side scripts. We've got a line that doesn't look like something we usually write in client side scripts. And that is calling a built in that we have access to called process. Which this gives us, so it's not the same, but in the client, in the browser, we can get access to the window where this is currently running.

[00:02:58] In the server side, we have access to the process in this system where this JavaScript stack and its heap and all of its stuff that's going on and all of the Node environment [COUGH] sorry, all of the Node stuff that's around it, is actually running. And that process object exposes a whole bunch of different stuff, you can read all about it in the Node.js docs.

[00:03:23] And by the way, we've now sort of exited the frontier of what MDN is our best friend for. So now we're gonna have to use other websites, which. Anyway, so the Node.js docs will give you all kinds of details about process and all of the things that it can do.

[00:03:44] And we're not going to talk about all of them. But one of the things it has is a cwd method, which stands for Current Working Directory, which when you call it will give you. The current working directory [LAUGH]. So, what we're doing then is calling our good friend setTimeout, which we can use.

[00:04:03] But similar to console is referring to a totally different thing than what it refers to in the browser. Which is the browser's built in timeout method. So Node again has like reimplemented this for us, so that we can continue setting timeouts all over the place. And in our timeout, we are just basically logging that string and we're waiting a second for that timeout.

[00:04:25] So that's what's happening. Tiny little script, but lots going on under the hood. And really decades of evolution of JavaScript encapsulated in these three lines of code. So yeah, so these are some things, we just have to keep in mind, some things are common. There's like a Venn diagram of APIs available in the browser that we're used to, like setTimeout and window.

[00:04:52] And APIs that are available in Node like process and setTimeout [LAUGH] and then ones that are available in both. And so in this case, setTimeout is the only one of these three that is cross platform as it were. But suffice it to say, this is obviously not an exhaustive list of all of the, well, perhaps obviously.

[00:05:13] This is not an exhaustive list of all of the things that you have access to out of the box, as it were, in Node. But keep in mind that not everything you're used to getting, like document, might be provided by the ever so loving Node developers. So keep in mind that we're now in a totally different context.

[00:05:37] And again JavaScript isn't really so good at just running without a surrounding context. Because it was developed to live within everything the browser was doing. So, when it got brought to the server side, a lot of that doing, if we wanna keep using the same principles and the same syntax and the same model, if we will, paradigm of programming needs to be reimplemented in a new environment.

[00:06:05] And now there's even more different environments where JavaScript can run. We're not gonna get into it, but Node is the most common one.