Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:12Length:1.9 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

1.2 Getting Set Up

In this lesson, I'll show you how to install Node.js on your system and how to write a simple Node script.

1.2 Getting Set Up

The very first thing that we obviously need to do is make sure that we have Node on our system. Now, if you've done any type of client-side development, where you have used NPM or Yarn, then you have some version of Node. However, it might be an older version, and you might need to update that. So, if you go to the command line, type node --version, if you have Node installed, it's going to tell you what version you have. If you don't have Node, then, of course, this isn't going to work. But you can see here that I have version 7, which, there's a couple of problems. One of all, it's out of date. Second of all, this is what is, I'm not going to say it would be considered a beta version. However, there are essentially two versions of Node. We have what's called a long-term support version, which is something that you would want to use in production. And then there is what's called the current version, which is something that has all of the new features. So, it's not something that you would really want to use in support. Because these new features are being added in these current versions, where they're being tested and hardened and things like that. So, I need to update my installations, so I am going to go to nodejs.org, and we can see that we have two options. We have version eight, which is the LTS, long term support, which, as its name implies, it's long-term support. Then, there is the current version. Now, the current version right now is version 10. Usually, the even-numbered versions are the LTS versions. So, I imagine that version 10 will become LTS somewhat soon. If you look at the LTS schedule, they give you a little bit more detail as to when these things will come into play. So here, we have version 8, we can see that it was initially released about mid-2017. The active LTS for version 8 started in October of 2017, and it will last until December 2019. The current version of 9 was initially released, and of course there is no LTS support for the odd version numbers. But we can also see, for version ten, it was initially released in April. And then, the LTS will start in October of 2018. So, it will be the next LTS version. So when it comes to installing Node, it's very straightforward for Windows and for Mac. If we look at the other downloads, we have several options, 32 bit and 64 bit, and there are some Linux binaries. And of course, the source code is available if you wanted to compile it and then run it yourself. And in some Linux distributions, that's probably what you would need to do. But as far as the installation is concerned, you just take the defaults and you are essentially done. So, for Windows and for Mac, it's basically the same, of course, there's going to be differences as far as the UI is concerned. You will, of course, need to accept the license and terms, we'll take all of the defaults, and you definitely want to add Node to the path, because we use the command line a lot. And we also want to ensure that NPM is installed. Basically, remember, we want to take all of the defaults. So, we'll just click next until we can install, and then we get all of that stuff done. So, after this is installed, I'm gonna close my command line, so that all of the newer executables will be ready to go, as far as my path is concerned. And then we can get started with playing around with Node. Now, Node give us what's called a REPL, R-E-P-L. It stands for Read, Eval, Print, Loop. And if you have done any work with PHP or many different SQL servers, or SQL engines, I should say, and many other platforms, there is a way that you can go to the command line, you can run whatever command. And then you can start typing actual code, and it will read, and evaluate, and execute that code. We get the same type of environment with Node as well. So, once this is done, which it now is, let's fire up the command line. And then, we will play around with this. So the first thing I'm going to do is make sure that I have the latest version. So, node version, and we can see that I now have a 8.11.1. So to get to the REPL, all we have to do is just type node. And then, that's going to give us our interpreter here. So, Node is a JavaScript environment. So we can type, really, any JavaScript expression, and it will evaluate that. So we can see 1 + 2, and the output is three. We can also create variables. So, if we create a constant of foo and assign it hello world, then we see that it's undefined, but that's normal. We can type foo and we see the value there. So we can also create variables, or not variables, but functions. So, if we wanted to create a function called sum that accepts a and b, and then it simply returns a + b, we can easily do that. We can call sum, pass in 2 and 3, and we will see the value of 5. So, we can use Node itself to play around and test some ideas. It's nice whenever we need to quickly test the behavior of something, or just see how something is going to work. If you want to exit, you just say either Ctrl-C, or you can say Command-C if you're on a Mac. Or you can just say .exit, and it will exit out of the REPL. So, the last thing that we are going to do in this lesson is create a folder, and then create a file that is going to essentially be a Node application. So, going to make a directory. Let's just call this Node from scratch. You can call it whatever you want. And, then I'm going to fire up my code editor and I'm going to create a file. Let's just call this first.js. Let's go ahead and jump into strict mode, because that's always a good thing. And we are simply going to say console.log. Hello, Node.js!. And in order to run this, we will say node and then the name of our file. So that was first.js. And it will run that file. We see the result. Hello, NodeJS. And we are done. So, we have Node installed on our system. We also have played around with the REPL environment, as well as written our first very simple application. In the next lesson, we're going to, well, do a lot of the same thing. We're going to create a very simple JavaScript application that we can run with Node, but it will give us a chance to start diving into some of Node's APIs.