Developer Productivity

Install Node Version & Split Up Tasks



Developer Productivity

Check out a free preview of the full Developer Productivity course

The "Install Node Version & Split Up Tasks" Lesson is part of the full, Developer Productivity course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen walks through using Ansible to install a specific version of Node, using tags to identify tasks, use tags to execute selected tasks, and separate tasks into files. Student questions regarding how to choose between using a bash script and Ansible and if Ansible can be used to install an operating system are also covered in this segment.


Transcript from the "Install Node Version & Split Up Tasks" Lesson

>> Let's say we have a new problem now, I need to install nodejs npm and node manager, and of course, node 14, that's what I like to use. So I have to do all those things, so let's do right now. Let's just take this little task, let's just install these things.

This is intentional, so hopefully everyone kind of realizes what I'm doing here, but let's go like this. Install node, and of course, to install node, I'm gonna use app, and I'm gonna go, what is it? It is name, it's gonna be nodejs, and npm, right? There we go, awesome, I now have it on my system.

Of course, right after that, I also need to be able to go, name, install and I use n for my package management, some people use nvm. nvm has that really horrible startup time problem. I've looked for a solution, I've tried a few different ones, they never seem to work, I couldn't stand it, so I just moved over to n.

Then we can just go shell, and go, or not shell, sorry, npm, and go, name= n, pretty straightforward. And then go, name and we can do, n14, right? And we'll just copy that, paste it in here and go n14, that will just simply install node version 14. All right, we've upgraded our script, and now have a node installation along with it, so let's go back here.

Let's do the docker and I also get version 14, which is what I wanted. Now I'm gonna go ansible.playbook up, hold on, I forgot one thing. Now that I think about it, let's go up here, and pre tasks. I want to make sure that I have all my repositories kind of up to date because if they're not up to date, I may get something out of date.

So I'm just gonna make sure everything, you know what, everything's good to go. So let's quit that, let's do my classic build dockers, jump into the docker container, brand new environment. Look at this, node doesn't even exist. So awesome, let's go, ansible, playbook, fem local, there we go.

Now it's gonna start running it's updating cache, excellent, this is exactly what I wanted to see, and wow, hold on, timeout, it's installing zsh. I don't wanna install zsh, I wanna install node. Well, lucky for you, we already have this planned out. So there's something called tags, tags allow you to identify any of these tasks with one or more kind of names to it.

So let's go like this, I'm gonna go tags and go zich. Awesome, so let's take this, and let's just go through here and paste it in each one of these little locations, there we go. Now on the node one, instead of using zsh, let's just call it node, there we go.

So now, I've tagged each one of these with node and zsh. I have to do one more thing which is kind of annoying, but it's just part of it. Just like any of the tasks, a pre task is just a task, right? It's just like anything else, my goodness.

This indenting problem is driving me nuts, so I'm gonna go zsh, there we go. So now I have tags for both node and zsh, so if I run zsh, it updates the cache, if I run node, it updates the cache. Fantastic, so let's close this down, rebuild our docker container.

I know I should have just used virtual amounts, but I wanted to be obvious that every time I make a change, I'm putting it in, whatever. So let's rerun ansible, now this time I can go like this, tag node, and then do fem local. Now when it runs, it's gonna update the cache like I wanted, but on top of it, it's only gonna be doing the tasks named node.

So this is really nice because if you have something on your system you need to kind of update periodically, say, building neovim from scratch, you can actually just have a task that ensures you have all the libraries are up to date, everything is good, and then builds Neovim every single time on your system.

Now you're probably asking yourself, why does node take so long to install? Well, it turns out npm relies on something like 500 different modules needed. Recall, it has to download so much, which is kind of ironic, considering everyone makes fun of npm for installing the world. So not only is node modules heavy, but to install the thing that installs node modules, it's also heavy, for me, it's beautiful.

So after it gets done installing node and npm, it's gonna go on and it's gonna install n, and then after it installs n, it's gonna break because you know what I didn't do? I didn't do that one one simple trick that doctors hate. Whenever you do anything with, I always forget this.

Whenever you do anything with npm, not only do need to specify a name, but you also need to specify if it's global or local. All right, so there we go. Why is there not a default? I don't know, that's unfortunate because now we have to do this beautiful little thing over again, rerun the docker, do the ansible, playbook, tag node fem local, there we go.

So now we're back up, we're running, we're doing it. We're updating the cache one more time, this is a brand new instance, remember. So of course, it's gonna do this every single time. It's going to forcibly reinstall everything. Npm is gonna once again take longer than I wanted to take to install, even with a great internet connection that I'm giving here.

And then once all those things are installed, it's gonna go to N. And then lastly, and most importantly, it's gonna install the version of node that I want. And for me, this is a big thing because you could actually specify many versions in here, right? So if I wanted to install 10, 12, 14 LTS, whatever I wanted to do, I could have them all specified within here.

I also now build Neovim using ansible exclusively cuz I just want everything to be up-to-date. It's super useful even just like in my semi monthly life that I have to do some of these tasks, I still have everything just to run through here at this point. So we're gonna give it a second, there we go, installing n takes a moment installing n 14.

Awesome, it is now done. Now remember, last time I ran this docker container, when I typed node, it was empty. It said, hey, that thing doesn't exist. Now when I type node, not only do I get it, I also get node 14. So hopefully, this is starting to feel a little bit more exciting.

Yes, it is a simple script that looks a little complex, but now you can start tagging things that immediately makes it a lot nicer, right? Now, you're like, okay, this is much easier. A little thing about ansible is that it runs from top to bottom. So whatever order the tasks are in, that's the order it runs.

But I think you can see, oop, we got a question, Ivan?
>> So my question was, when do you decide when to use ansible and when to write a bash script?
>> So my general rule of thumb is, anything that is installable, I just use ansible universally. So whenever I have to use a new tool, so I actually have an Ansible script and both for my personal life and also for my Netflix life, right?

And other people can use that same Ansible script at Netflix to get up and running for that specific team, which is actually very convenient because we have hundreds of teams, and so I have to work, unforced them in one of those positions where I work on about four or five different teams.

And so I'm constantly having to have other people's environments all the time. And so having something that can get me up and running, and so I just don't forget all the things super useful. So I put everything in Ansible, I can, except for just even installing the dot files.

But I have, all are in Ansible, but how I install dotfiles, I use a different program. I don't manually specify each link in Ansible. I feel once you got into that world, you've probably maybe went too far, right? You shouldn't probably be writing shell and just doing raw scripts constantly, there should be some sort of plugin, there should be some sort of built in.

I just use npm, and then specify the name if it's global, right, that's a good thing for Ansible to do. Obviously, like going back to that said part, right, at manually installed the szh auto suggestions using said, to me, that was me breaking really Ansible, and just kinda turning it into just a raw script engine.

So probably don't do that, but hopefully you can kinda see, does that sufficiently answer your question, Ivan?
>> Yeah, definitely, thanks.
>> So you're probably seeing where I'm going with this, there's way too many tasks going on right now, right? This file is going to get, it's just going to be gigantic, right?

This is gonna be super painful to go through, I don't wanna have to deal with that. So let's pretend like this doesn't exist, and that I definitely deleted that before hand. And my goodness, there is a task directory that I have right here. And so I'm gonna open up this task directory, and I'm going to create something called node YAML.

And then I'm gonna create something called zsh.yml. I'm gonna save it also. And we're gonna go back to our main file here, and I'm gonna go up to my tasks, and I'm gonna go all the way down to the point where node is being used, and I'm gonna just delete all these.

So that's all I've done, is just deleted the tasks, I haven't changed anything else about the file. I'll save it, jump over to my zsh file, and move it back one, and that's it. Go back to the main file, go down through node, delete up the nodes, go to my node file, and put those right there.

There we go, everything's aligned, we're looking good, we have my node with the node tasks, we have my zsh with the zsh task, we just need one more item. I'm gonna go back to here, and I'm gonna go include zsh_yml, but it's actually, what is it? There we go, and then I'll do the exact same thing, f to z, go like that, node, perfect.

Now I've included those two files, so we've taken this really big file and turn it into something small. Now we're breaking up tasks and making them smaller and smaller, so that it's really easy to understand. What do I do when I'm installing node? I do this, I could add in here, Type Script, Type Script language server, I could add in a bunch of those items, so that way each time I install a new version of node with n, I also install along with it, each version of the corresponding npm modules, right?

You can go really in depth in here, but we're not gonna do that for now. So there we go, we have that all running. And of course, if I were to redo the build-dockers, redo the docker run, do ansible-playbook. And this time let's do tag zsh because it's just faster, and then run fem.yml.

You'll notice I keep saying a fem.local, I'll explain why here in a moment, it's just on my brain. It will go through, notice, it updated the cache, it's installing zsh, changing my shell, getting on my zsh, having everything running, I'm good to go. And now it's way more easy to use that script, and look, it works, auto suggestions also work as well.

Awesome, it's still running, everything is running exactly as expected. I can get a clean system now, something that has no libraries on it. I can get it up and running in a matter of seconds. The reason why I keep saying that local business, is that if you look at the files that I have, one of them is called, Local.yml.

So if you have a repo, and let's just go back to, here go ansible. Do I have it? Yeah, there we go. I have this as a repo, right? It has a bunch of stuff in here, you'll notice, what does that dangerous SSH folder. But anyways, I have it all on here, I have a file called local.yml.

If I wanted to install this repo, instead of doing ansible playbook with the files already on my system, I can do ansible.pull with all the same key commands. So I could literally do ansible.pull instead, and provided a URL which would be the URL to my Git repo. And it would do the rest for you, meaning that you don't even have to have the thing cloned on your system.

You just have to have Ansible installed which takes a few lines of installation. And that means even more so, I actually have this little script right here, Ansible run, which just does it all for me. I don't have to do anything, right? All I have to do is just go to GitHub, go to my public repo, get this script in the raw, I can copy and just either paste it directly into my system, or I can just curl it down and type or write to SSH or SH and it'll just work, right?

It just simply works, and I can have my entire system boot strapped in a matter of moments. And for those that don't know, I use a custom keyboard layout. So when I have to type in QWERTY by the way, I use Dvorak, by the way. When I use QWERTY, as I like to call it, which is gonna be of course, they are your QU, whatever that thing is called.

All those fancy symbols that people use Q-W-E-R-T, right? That's your top row, I don't even have to worry about that, everything will be up and running and working, and my keyboard will be all software remapped and everything, and so I'm just ready to go right away. All right, Ivan, you have another question?

>> Yeah, can it be used to install the operating system itself?
>> You can install i3 and other desktop managers or window managers and things like that. I've never used it to install an operating system. I don't think you can do that because Ansible kind of assumes there already isn't an operating system itself.

Unless of some operating system, I've never installed arch, so I don't know if you can install arch from Ubuntu. My guess is that you can't do that, you're gonna have to go through grub and the whole nine yards and get your system partitioned, and have everything set up and boom, then you're ready to go.

So I don't think you can do that, but once you have your operating system, if you're on Windows, you launch the Linux subsystem, and just pull it down right at the rock, everything's ready in Windows environment, math, exact same way. The thing I'm not showing right now is that Ansible also has a with clause.

And a with clause means that I can say, hey, with this operating system, do this, with this other operating system, do that. So that way, you can even have operating system dependent commands, because obviously your Mac installation might be slightly different, not on zsh, but say on your node package manager.

Because you're gonna be using brew instead of apt. And so there's a lot of these little tricks you can do to make it really straightforward.

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