Check out a free preview of the full Introduction to Node.js, v3 course

The "Hello World" Lesson is part of the full, Introduction to Node.js, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Scott walks through the setup of Node.js and a version manager called nvm. The segment includes creating a simple Node.js program that prints to the terminal.

Preview
Close

Transcript from the "Hello World" Lesson

[00:00:00]
>> All righty, okay, let's write some code. I've been talking too much. I just wanna write code. That's what I'm here for. So let's write our first Node.js program. But in order to do that, we need to install it. So there's many ways to install it. I already have it installed.

[00:00:19]
You can walk through what I'm about to show you to install it. You can also just use something like CodeSandbox in this example. And most tutorials probably would just use something like CodeSandbox, but I wanna walk through how to install Node. I think that's important to know. If you're gonna be using it, know just how to install it cuz there's many ways to do it and there's some gotchas.

[00:00:37]
So I'm gonna walk you through my preferred way, which is using something called NVM. So NVM stands for Node Version Manager, and it's exactly what it sounds like. It manages your versions of Node. So if you wanna use NVM, you can actually head over to their documentation. And you can copy their install scripts.

[00:01:01]
I believe this install script is universal and it works pretty much any machine here. So you can copy this one right here and run that in your terminal. And then once you have that, depending on your setup, whether you're using Bash or Zsh or whatever, you wanna add, you wanna copy this and add it to your specific setup.

[00:01:26]
In my setup, I'm pretty sure I'm using Zsh, so I might have this in my Z-S-H-R-C file. This might be your Bash profile file. If this sounds too complicated to install, don't worry about it [LAUGH] we'll go the other way to install something. It'll be some gotchas there, but it'll work for today.

[00:01:45]
This is just my preferred method. And the reason I like NVM is because it allows you to switch versions. You can literally change a version of Node whenever you want. Whereas if you go through the Node.js documentation website and install it that way with the installer, you'll get the latest version, but you can't switch versions.

[00:02:01]
And then there's a lot of security issues around how that installation access files on your computer, which makes installing things kinda complicated sometimes. So to avoid that whole issue, I just use NVM. So you have NVM here. Once we have that, you should be able to go to your machine and do something like nvm, and if you hit that, you'll see some version.

[00:02:28]
If you get an error here, that means either a, you didn't do this part correctly. Where you paste this into either your bash profile, your bashrc, your zshrc file. And which, by the way, once you do that, if and when you do that, you can't use the same terminal window because it needs to reevaluate that file.

[00:02:51]
So the best thing you can do is you can type in source, followed by the file that you just edited, whichever one you added that to. Or the easier way is just open up a new terminal tab, and that will reevaluate that file. Cuz you can't use the same tab that you were in when you added it.

[00:03:06]
It won't know that you updated that file. So once you have it, you'll see nvm version. Now, if you're having trouble with this, then the easy way to install it is just to go to the Node.js website. Gotta make sure I Google that cuz I think someone owns the Node.js.com or something like that, and it's an inappropriate website.

[00:03:33]
So I never [LAUGH] never go to, it's always just Googling to make sure I got the right one. But you just click right here, right? It'll detect what system you're on. And you can just click this one here, 18.6.0 LTS. LTS stands for long-term support. Basically, every other version, they have a long-term support version in which they plan on supporting for a while.

[00:03:58]
But then every odd version, it's not a long-term support version and it's gonna get thrown away. So almost never installed the odd number ones. I always just install these. You can do 20. I just haven't tested this app on 20, so I don't know if there are any breaking changes.

[00:04:13]
But we're gonna use the latest of 18.16.10. So you can click this, it has an installer. It's very easy to install. It'll do all the work for you. And then once you have that installed, you should be able to do node version and see what version you have.

[00:04:31]
I'm a little behind with 18.14, but that's a minor version, so it shouldn't be any difference between if you had 18.16, so don't worry about it. Okay, hopefully, everyone had enough time to get Node install, whether it was with NVM or from the Node.js website. If you did do NVM, just make sure you also, once you have NVM installed that you then install Node using nvm install followed by the version you want.

[00:04:57]
So that would be nvm install 18, and that would install the version 18, and then you should be good to go. Okay, so once you have that going, you can double-check, node --version, and you can see what version you have. It'll also tell you the path on which it was installed, things like that, depending on your terminal.

[00:05:18]
Now, what we can do is we can write our first Node.js program. So I'm just gonna make a new repo here to folder. I'm just gonna call it intro-node. Okay, so many things, there we go. And I'm just gonna open this up in my VS Code. And I'm just gonna make a new file here, call it index.js.

[00:05:52]
You can call it whatever you want. I'm just gonna call it index.js. And I'm just gonna write some JavaScript that you've probably already seen before. I'm gonna write console.log. And I'm just gonna say, hello world. I'm gonna save that. And then once I do that, I'm just gonna say node and then the path to that file.

[00:06:13]
Seeing how this terminal is already in this folder, I can just type index.js and execute that. And then I should see the output hello world in the terminal. So what's actually happening here? Well, this is how you execute JavaScript with Node outside of the browser. Before you typically would have the written code in a script tag or another JavaScript file included that in the HTML file, go to the browser, let it run, open up the console, and then you can see it.

[00:06:44]
Or you could just open up the console and write JavaScript in there, which is not a good experience, but that's how you had to write JavaScript and evaluate it and execute it before. Now you can just write it in a file and point to it with this node command here in your terminal and it will execute it.

[00:07:00]
So any arbitrary JavaScript you can write. So if you go in there and write some things, run it, you'll see what I'm talking about. It's pretty simple. Also the other thing to note is that we were able to use something familiar, console. Whereas before in the browser when you do console log, that actually goes to the browser console.

[00:07:18]
Whereas in the terminal, the console is the terminal. So in most languages on the operating system, you might see this as print. It might say something like print, print line, or print something. Whereas in JavaScript, it's still console because JavaScript was made in the browser where there was a console.

[00:07:35]
So when they brought it over, they decided to just keep it console, but it's the equivalent of print and other OS level languages, if that makes sense. Any questions on that? Okay, and yeah, I mean, get in here. See what happens if you try to do something that you normally would do in a browser.

[00:07:54]
Anyone knows, anyone wanna guess if I did something like alert what might happen?
>> I can guess.
>> Yeah.
>> You're gonna get a dialog box in the browser. I guess I don't know what would happen if you run it in your terminal, though.
>> Right, so yeah [LAUGH] exactly.

[00:08:15]
So what do you think would happen if I ran this on a terminal?
>> Nothing?
>> Let's see, so I run this in the terminal. Boom, I get an error, alert is not defined, and that's where we start to see the differences. Like I said, there are some things that carry over, like the console, but there are other things like an alert that doesn't.

[00:08:38]
And the reason something like an alert probably wouldn't carry over because alert does something visual, like you said, a dialog box. There's no visual output for terminals that's text based. So you probably wouldn't get an alert on the terminal. I guess maybe it would make sense to get a desktop notification or something.

[00:08:55]
And I'm sure someone's made a package for that, and we'll talk about packages and modules later. But yeah, by default, we're not gonna get that. So this is where being in JavaScript is the same, but it's in a different environment. So it has access to different things. So alert is not one of those things that it has access to.

[00:09:14]
Does that make sense? And this is what an error looks like, very similar to what an error looks like in the console in the browser, cuz it's still just JavaScript. You still get a call stack and things like that. Don't worry about this module stuff. We'll talk about that some other time, but yeah, it's just an error.

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