Check out a free preview of the full Vanilla JavaScript Projects course

The "Introduction to Node.js" 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 introduces Node.js as a way to run JavaScript outside of the browser. Developers can write JavaScript that runs server-side or in the command line. While some APIs are similar, Node-based applications can access system-level resources and other APIs not available in the browser. The Node.js REPL (Read-Evaluate-Print Loop) is also demonstrated in this lesson.

Preview
Close

Transcript from the "Introduction to Node.js" Lesson

[00:00:00]
>> Let us talk, so far we've been talking about JavaScript in the browser. And we've been getting a lot of great life lessons about all of the many things that are happening in the browser that are not JavaScript and the fact that JavaScript is typically client side. JavaScript is running within that whole magical universe of mystical, incantations, and event loops and built in API's and so on and so forth.

[00:00:26]
But, That was 1995 JavaScript when JavaScript only lived in browsers. And now, in 2023, or whatever year it [LAUGH] happens to be, in 2023 Very JavaScript lives all over the place. And, so that is where our, our friend who you may be acquainted with already, Node.js comes in.

[00:00:50]
So Node.js, lets us do some JavaScripting without that browser context of all of the magical, wonderful, thank you browsers, things that are going on. So, [COUGH] Node.js some folks have maybe written and worked with node before and used it, but just as a review. So, [COUGH] Node.js, which you can read all about in like thousands of blog posts on the internet.

[00:01:17]
Is essentially, In an attempt to allow us to run JavaScript, which again was developed as a language to run inside of the browser to do things without a browser. So to do things on the server-side, or perhaps not even on a server, but command line tools and things like that.

[00:01:39]
So it's a way for us to take our skills as JavaScript developers and bring them outside of the client side environment and into all kinds of other places. And so, one downside of that is like all those awesome built in browser things that we've now kind of gotten used to working with.

[00:02:01]
If we're not in the browser anymore, we don't have all the browser magic and all of those built-in APIs and all of those things. But one of the upsides is that node has its own things that the browser, you know, may or may not provide in terms of things that.

[00:02:20]
For example, will give us access to the underlying system the user's, operating system, file system, all this stuff, right? When we're outside of the browser, we don't have that layer of kind of indirection of talking to the outside environment of the user's computer. So it's a trade off, right?

[00:02:40]
We're, we're losing some of the browser front end convenience, but we're also gaining some cool things that we can't do client side. And [COUGH] there is a fair bit of the some of the most commonly used browser conveniences that make JavaScript go like the event loop. That have been sort of re-implemented in node.

[00:03:03]
So node has its own event loop so that we can still use the mental model of [COUGH] asynchronous JavaScript, and events firing, and all of that. Even though we're not talking about browser events necessarily. And there's also implementations of things like set to timeouts, right, which now the underlying implementation is gonna look different than set timeout in the browser.

[00:03:25]
But we'll still be able to use some of those same features and whatnot because node has re-implemented them for us, essentially. And so, and just a note in terms of the engine that's running, so we talked about how there's different JavaScript engines, like V8, like SpiderMonkey. So V8 is essentially what's powering the JavaScript in node.

[00:03:46]
And so that is where you will often, like when it comes to diving deep on the performance of node and things like that. Often you'll end up needing to learn more details about V8, so good to know. Hope we all like drinking our vegetables. All right, so, do folks have node installed?

[00:04:08]
If you already do, fabulous. Maybe check what version you're running and make sure you've got the latest LTS. Which I wanna say is 2010 at the time of filming, but I'm not sure, so check. And you can download this, if you don't have it, from nodejs.org. I think it will very helpfully immediately point you to [LAUGH] the downloads for your particular system.

[00:04:34]
So, if you don't have it at the moment, it's a great time to go to nodejs.org. Grab that download and get it installed on your system and it's usable on Mac, Windows, Linux, all of the things. So there is a node for you. So you know that you have node if at the command line, you can type the node command.

[00:05:02]
And it knows what you're talking about [LAUGH]. And so, for example, there's a version flag that you can use to check which version you have. And if you just type node without anything else, you'll get an interactive rebel. Read evaluate print loop of JavaScript that's sort of similar to the console in the browser.

[00:05:21]
That lets you do quick checks of, like, does that thing have a this thing? And the type that we've been doing in the console. Okay, so we're gonna be switching gears here to stop looking at such an elaborate thing. We could be looking at the terminal in VS Code, but we're just gonna look at a terminal Window.

[00:05:41]
And so now we're gonna enter the command line, world where we're gonna be doing a bunch of things from our command line. Now, is that the only way to interact with node? Is that the only way to interact with some of the tools that we're gonna be using today?

[00:05:57]
No. But it is most common and like in terms of the documentation that you'll find for all kinds of tools out there and libraries and whatnot. They're basically all gonna give you commands to run at the command line, so we're gonna do this here. And yes, you can open a terminal in VS Code to do this if you prefer to have that.

[00:06:20]
But I'm just in my computer's terminal saying, all right, so let's make sure that I have node cuz honestly. Who knows at this [LAUGH] point? [LAUGH] So yeah, so I just type node and here we are. So for example, one of the things that node has reimplemented for us is a console.

[00:06:38]
Cuz remember how the console and the document all of that stuff was on the Window. Well, we don't have a Window, so luckily the [LAUGH] developers have do not have, like, reimplemented a lot of the wheel here. Or whatever that saying is that allow us to do our good old friends, like log hello, world to the console.

[00:07:03]
And just like in the browser console, we get hello world and then the return value which is undefined. So and I can control C to get out of my twice to get out of my rebel here. And we're probably not gonna spend a ton of time in that rebel but it is a nice way to just quickly run a line of code.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now