Introduction to Node.js Introduction to Node.js

Checking Install & Running Node.js Solution

Check out a free preview of the full Introduction to Node.js course:
The "Checking Install & Running Node.js Solution" Lesson is part of the full, Introduction to Node.js course featured in this preview video. Here's what you'd learn in this lesson:

Scott troubleshoots some common issues with Node.js installation, and demonstrates how to execute a file.

Get Unlimited Access Now

Transcript from the "Checking Install & Running Node.js Solution" Lesson

[00:00:00]
>> Speaker 1: Welcome back, I hope everybody had a chance to install Node. If you use the NVM approach you've and you haven't set up like a proper environment on your machine. Like bash our seas and bash profiles you might have ran into some issues with the variables being exported so I can walk through some of that.

[00:00:17] And also I saw there were some issues where, you have Node already installed from source or from Homebrew or from whatever way you installed it, and then you use NVM to install Node. So now you got two versions of Node from two different sources running at the same time.

[00:00:33] So how do you get around that, and how do you know which one you're using? I'm gonna show you how you can get around that, but this is gonna be from the perspective of a Mac OS machine. How you would do it on the Windows? You would just have to know the appropriate commands but I'm sure it's the same thing.

[00:00:47] But basically, if you go into your terminal and on Mac if you type in the which command, right? You can type in which node and it'll give you a path. So, you're using the nvm one when in the path you see in the nvm, so I'm using the nvm one.

[00:01:01] I also have node installed from source on this machine, but I don't use it because I'm using nvm one. So to understand which one you're using just type in which and then the the tool that you want to see and it'll show you. And this works for pretty much every CLI, but we're just using it for Node.

[00:01:17] So this is how I know I'm using nvm. If you see something like, again, if you're like a Mac user, casket, if you use like Home Brew. Pretty much if you see anything that doesn't have nvm in it, then you're using the other one that you installed prior to this course.

[00:01:30] So the way you would get around this, is actually if you installed it and the nvm they should took care before you, they should have just did it for you. But if not you can just say in the end, use and then the name of the word, then you should be good and then set the default, and then you should also be good.

[00:01:45] And then if you wanna go back to using the other version of Node, and some other terminal of which I don't recommend. Then you can permanently disable nvm by getting rid of the exports in your bus profile, or you can temporarily disuse the other version though by using the full path to it, which is an executable.

[00:02:05] So you just have to type out the full path, you can write a shortcut for that in your bus profile. So that's how you get around the two versions, but what I recommend doing is if you have another version that you installed prior to this course with Node nvm then just download that exact same version with nvm and then have that version too.

[00:02:22] And just never use the other one. So that way you have the latest version that I recommend that you install. You also have the version that you already had installed but with nvm now, and you have both of those versions. And then just never sue the other one from source, just highly recommend never using it, it's just not fun.

[00:02:40] Yeah, and then as far as the bash profile stuff, I don't wanna open up my batch profile cuz I got a lot of secret stuff in there. But basically on Mac, you have a bashrc and you have a bash profile, right? So those are just like, you can think of them as config files for your terminal, and your environment.

[00:03:02] When nvm installs itself, it exports variables so your machine can understand where nvm is located. Those variables are just a path., like here is a path to nvm, here is a path to Node. Those have to exported so your environment can use it, without it if you type in nvm you get an error, it's like nvm is undefined.

[00:03:21] If you get nvm is undefined it's because you haven't set things up properly. So basically all you would have to do is open up your dot dashrc file, which is on the root of your computer. It will be root dot bashrc, if you open that file up you'll see that mvm exported.

[00:03:37] It puts some code in there, it's like export nvm, it did some stuff. That's good, that's a good sign. And then all you have to do from there is open up your bash profile underscore profile like this and if you don't have one just make one. You open that up, and in there, all you gotta do is just source the bashrc file, which basically means read this file.

[00:04:00] That way, when a bashprofile file is read, which happens every time you open internal window, it'll also read the bashrcfile, which will then export those nvm variables. So once you do that you should be good. So again open up the bash profile file and type in source bashrc like that.

[00:04:18] So you just type that in the file and then you're good to go. Any questions?
>> Speaker 2: Just a note maybe for Linux I think it's backward on Linux, Linux sources bashrc In which you can source your batch profile.
>> Speaker 1: Right, yeah, it is backwards analytics, yeah. Any other questions on installations?

[00:04:35] Was everyone able to run the Node riffle? Yep, awesome. And did everyone create a JavaScript file and try to execute it? Did anyone have that file execute, and then fail, like in my example? Nope, you wrote perfect JavaScript. Isn't that great? Like, you just wrote perfect JavaScript and it worked perfectly, right?

[00:04:55] That's how sweet Node is. Most of the time it is that case, like you just write regular JavaScript and it just works. But, as you're about to find out, there is some stuff that you can't do in Node that you can do in a browser and it just will not work by default.

[00:05:07] But, it is Node and you can virtualize pretty much anything. So, you can make anything work, but by default, it will not work. So if everyone is good with installing stuff then let's move on to the next piece here. So we are gonna make a new file here and we'll just call it code.js and we are gonna make it right here.

[00:05:27] And then what I'm gonna do is I'm going to open that
>> Speaker 1: Here. And we will just make some code, I don't want to update, don't make me update. So we will just say console.log hello from node. So that is just regular javaScript, nothing fancy, regular console.log. And then, what I'm gonna do is I'm gonna go back to my terminal.

[00:05:56] I'm gonna use the node executable, and I'm gonna type in the path to that file. And if I check it out, don't list those. If I check it out, that file is literally right here, it's right here. So what I'm gonna do is just node And then the roads are path from this location to that file where it's this code.js.

[00:06:16] Cuz I'm in the directory of this file, it's the relative path. And then you can see hello from node. It just executed that file and then it immediately exit it. So that's something important to watch is that it executed the file and then exit it. Most, some applications that you're gonna write are not gonna exit.

[00:06:36] That is gonna be there forever and that's gonna be intentional. Like APIs, you want them not to exit, if the API exit then it broke. So we're gonna figure out how to do that. But yeah, for this one of synchronous lines of code that we wrote, it's just gonna go and stop.

[00:06:52] Cuz it's all synchronous, there's some top to bottom, there's no async stuff that's gonna be done.