Introduction to Node.js, v2 Deployment: Packages
Transcript from the "Deployment: Packages" Lesson
>> Now that you can develop with Node js, we built APIs, we built COIs, we built things that interacted with file, which are arguably the top three most use cases of Node js. You need to learn how to get your apps off your local machine. If you're building a frontend app, you deploy it to a CDN, you build it.
[00:00:20] If it's a server rendered thing, you throw it on Heroku, reverse cell, or Netlify, or whatever. You get it off the machine, you deploy it to some other machine somewhere else. Well, whatever you're doing with Node is gonna determine how you deploy slash publish it. So, let's talk about, let's say you're making a package.
[00:00:38] So, if we go back and look at some of the things that we worked on today, some of the packages were the CLI, that's the package. Maybe this file reading, that's a package. Everything here is a package except for the API, that's a server. So you can't deploy a package, it doesn't run.
[00:00:55] It gets executed on demand. There's a command you run and it gets executed and it stops. It's expected to be executed by a human. That's why there's a CLI. There's no programmatic interface with doing some stuff that we did, they're little scripts. So you can actually deploy that to a server and have it hosted, that wouldn't make any sense.
[00:01:11] So instead, you're probably published those. So that way, someone can NPM install your thing, right? So, I'm gonna walk you through how you can do that with NPM. And there's a lot of ways you can do it, but I'm just gonna walk you through how you can do it with NPM.
[00:01:24] So, if you wanna publish something, you want to be available to be downloaded, this is how you would do it. The first thing is, obviously, make sure you have NPM installed, which at this point I hope you do, cuz I don't know what you've been doing if you didn't.
[00:01:36] So I hope you have NPM installed. The next thing is you need an NPM account. So if you go to npmjs, And sign in. I don't even know what mine is, but I know I got an account somewhere. So you need an account. And then, the next thing is you need to log into your account via the NPM CLI.
[00:02:03] So you can type in npm login. Hopefully, I'm already logged in. Of course, I'm not. Now, it's gonna ask me for the password that I don't remember Okay, you want my email too? Okay, yeah, obviously, I don't know my password is. Let's try this again. Okay, I don't want you guys watching me try to get into NPM right quick.
[00:02:44] Let's see, I'm gonna try to do this without the login. Let's see what happens. That way, we can at least run through it, and then I can reset my password if I need to. But let's try to just do NPM publish on one of the packages, and we can at least run through the reps.
[00:03:01] So, let's try to deploy this reddit-cli. So it's got everything we need. It's got the gitignore. It's got the package dot json. It's got this reddit-cli, but that name is already taken. So, I'm gonna change this to scott-reddit-cli. If someone has that name, they deserve it. I'm just gonna give it to him.
[00:03:23] The main is gonna be index dot mjs. And which obviously doesn't matter because it's a CLI, but whatever. And we should be good there. So now, I'm going to CDN to reddit, and I'm going to say npm Publish. And just like that, it published it. So, it went ahead and publish scott-reddit-cli.
[00:03:52] We are good to go. You should actually be able to install that on your machine right now if you want to. Go ahead and npm install scott-reddit-cli, and you can get access to it. In fact, I'm gonna do it right now. Npm install scott-reddit-cli. And it breaks out because I'm already inside of a package called scott-reddit-cli, right?
[00:04:15] So, let's go into another one like intro, I'll run that again. And boom, I just installed the CLI into the intro file, and I can verify that. There should be a bin folder in here, there is. And it should say reddit, it does. And it should be my code, and it is.
[00:04:32] So, I was able to install that. That's simple with NPM. The whole world has access to it now. So, that's how you published the MPM. And you should be, and the reason I want to log in, so you can go and you kind of manage all your different things.
[00:04:47] But apparently, I'm already logged in or it doesn't really care. I don't know. But yes, that's how you publish on NPM. You can't take down versions you already published. But you can just publish new versions. And I do recommend using the NPM CLI to do your versioning. So you can do something like npm version.
[00:05:08] Let me go back to the reddit-cli. Then you could do npm, what is it? Is a version. No, it's npm Bump. No, what is it? Let's see. I don't use this one that much, I use a bunch of automations that make this not as easy or a lot easier.
[00:05:36] I will just show you the one that I use. So I use something called semantic Cli, Semantic-release-cli. So, I use this tool to help me with the versioning of my packages. It basically will look at my commit messages. Whatever type of message I wrote to determine what the new version of my package is.
[00:06:02] So, I do have to write my commit messages in a very strict way. But I'm used to is I do a lot of open source, and they make you write that stuff anyway. If you ever contributed Angular or React, they have a very specific commit format that they want you to follow, what they're messaging and everything because they use tooling as well.
[00:06:18] So, if you do write your commit messages in certain way, you can have a tool like semantic-release. Look at those commit messages and determine do you need a minor number upgrade or patch or major? It'll look at those things to determine. It'll generate the new version number for you, change it in a package that JSON, and then you can just publish it.
[00:06:37] So that's pretty much what I use to get my packages published. Although, I haven't really published any packages in a couple months now. But when I was, that's what I did, and it was amazing. And then, because all that's automatic, you could put that into CLI. So you can just, if you merge something into the published branch, then whenever it gets merged in there, the CLI or the CI will take care of that for you.
[00:07:00] So you don't have to publish on your own machine. So, automate all the things, why not? So yeah, I highly recommend looking into some flow like that if you're serious about publishing on NPM.