Check out a free preview of the full Introduction to Node.js course:
The "Deploying to Heroku" 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 demonstrates how to deploy the module to Heroku, and describes how to publish to npm.

Get Unlimited Access Now

Transcript from the "Deploying to Heroku" Lesson

[00:00:00]
>> Scott Moss: All right, cool. So we did have a last exercise, but this is more of a like, do this if you want type of thing. It's not something that there's no test or nothing associated with it. It's just like, take one of these and deploy them if you want to to a provider, cuz I don't wanna get into DevOps stuff and specific things, and having you sign up for npm account.

[00:00:18] So if you wanna take one of these exercises, feel free to deploy it to Heroku or npm. But what I wanna do is walk through how to do it, so at least you have an idea of what it looks like. So what I'm gonna do is, I'm gonna walk through how to do one of these on one of these providers.

[00:00:32] That way, you kinda get an idea of how it looks because it's gonna be very generic, but it's gonna be very specific to whatever you do things. So first, what I wanna do is, I guess I could deploy this API to Heroku. So again, it's gonna be depending on however the car provider wants you to do things.

[00:00:55] Heroku is pretty basic and it's almost basically free, so okay.
>> Scott Moss: All right, and then basically, make a new app. Create a new app here, we'll give it a name, test-frontandmasters. I'll make myself the owner and I'll just create the app. Well, and you can see Heroku automatically starts giving you instructions.

[00:01:27] So it asks you to log in with their CLI, so we go, there's CLI, log in with their CLI, I need a projects, and then you gotta add this. So I'm just gonna go ahead and add this cuz I already have most of this here already. So I'm gonna say, heroku git:remote -d test-Frontendmasters.

[00:01:45] Boom, that did the job there. And then now, it says, I can just deploy it. I can promise you, once I deploy it, it's not gonna work cuz it doesn't know what to do with it. But just to get it up there, yeah, I can just deploy it like this.

[00:01:57] So basically, with Heroku, you just push it to Heroku like this, git push heroku master. I just,
>> Scott Moss: git push heroku master, it's gonna take this code from Git and just push it to, Heroku's get repository up there. And you can see, I'm getting this log, it's trying to build it, it's about to do some stuff, it's gonna try to deploy it, and it might, but then it's gonna try to start it, and it's gonna fail, because it has no idea how to start this app.

[00:02:25] It's like four different apps in its repo, it doesn't know what to do. So it's gonna fail there, which you can see, this is the process that it's going through, trying to download things. You can see it did a yarn install instead of a npm install. And it's caching the Node module for faster builds next time.

[00:02:39] It's running into some issues here, and it looks like it succeeded, but this app didn't tell us how to start anything, so we didn't really start anything, but hey it's done. And that's how you deploy it, it's that simple, that's deploy in Heroku. So the other thing you have to do is you have to tell it how to start, and with Heroku, they have a thing called like a Procfile, Procfile, like that.

[00:03:04] And then you would do something like this. So I mean, look it up, Heroku Procfile Node.js. Getting started, Node.js Heroku. Ready to start.
>> Scott Moss: Yep, did all that. Prepare the app, need the Procfile, deploy my app for sure, got my logs. Okay, just show me out this Procfile, bro.

[00:03:39] Here it is, so there you go. So this is how you tell this. Very cool, I want to use web and then you tell it the entry point to your app. So in this case, it would be server.js. So there's one thing I need to change, and that's gonna be the hard-coded port.

[00:03:54] So instead of using the hard-coded port 3000, I have no idea what port Heroku is gonna assign to this app when I deploy it. I have to change that to process.env, that's an object with all the environment variables on it, and Heroku's for sure, I will put one called PORT, all caps.

[00:04:08] So I was gonna put all caps here ,and host name is probably gonna be provided too, so I'll have to change that as well to whatever environment variable that Heroku injects into the environment. So once you do all that, then yeah, you can just deploy it again.
>> Scott Moss: 'deploy', and you can just push this up to Heroku and it'll try to do it again.

[00:04:36] I'm sorry, this is pushing straight from master, so it's being weird, git push heroku master:start. There we go. Okay, it doesn't like that.
>> Scott Moss: And the co master, and then yeah, I gotta do all that Procfile stuff here again. I'll start to get that going, but yeah, that's basically what you gotta do.

[00:05:07] I don't want to continue switching to get branches and do all that, and that's deploying Heroku. It's pretty simple. It's that's it, you don't have to do anything else. But obviously, there's more stuff you have to do like scale it, if you are gonna add databases, and so there's so many more things you wanna do, and that's why I don't want to get into all that because there's so much stuff you have to do there.

[00:05:25] But yeah, that's the point there. And therefore, npm, npm just have tons of tools here to actually package things out for you. So once this is on get with npm, all you have to do is get this npm publish, and I'm not gonna run this cuz it's gonna publish it.

[00:05:37] But once you run npm publish, it's going to try to publish this to npm. Actually, what I'll do is just to show you that it works. I'll put a name that I know is taken, lodash, and I'll just type npm publish. And you can see right now, it was like, that package requires a publishing.

[00:05:57] So yeah, it's like, yeah, this one's protected, dude. You can't do it, sorry. So that's how easy it is to publish on npm. You literally just run npm publish. But there's this work you have to do ahead of it, like changing the version number, adding a readme, updating your change log, doing stuff like that.

[00:06:12] Maybe you can add in every release on GitHub that corresponds with the right version. There's so many things you wanna do there. So it's not the publishing that's difficult, it's the work before the publishing that's tedious. So normally, people automate this task for them out of the CLI environment.

[00:06:25] But yeah, that's how you would do that, and that's how you would share your modules. You either publish a module to be installed by npm or Yarn, or you deploy it to something. That's basically it. You just don't email people your modules. You do one of those things.

[00:06:40] Yes?
>> Speaker 2: How common is it for fancy, well-established firms, I'm sure the cat, the swirly cat on your shirt, to consume your own npm modules that you publish, consume them as if they were public modules.
>> Scott Moss: All the time. Because, so those are what we call private modules.

[00:06:59] So we have like many apps, and there's some code that we want to use in all the apps. So instead of writing them in every single app, we write down once in its own app, we publish it to npm privately, and then any app that we need to use it will just install it.

[00:07:14] But it's private, only we can use it with this token. So most of the time, when you install a private module, you'll get a name space, like for us, our name space is @tipe. So anything on npm is, the name space @tipe followed by the name of the package.

[00:07:26] So we'll just say, npm install @tipe/ the name of whatever it is. So you'll see this a lot now in npm @ name space, so like @angular is their name space. So that's most of the time cuz it's either private and/or scoped. That's called a scoped module, and by default, all private modules are scoped.

[00:07:46]
>> Speaker 3: So we do have a full stack course by which kind of covers the sort of production side of deploying node on setting up digital ocean, nginx, and all that kind of stuff.
>> Scott Moss: Yeah, take that one, that would definitely go way more in depth of everything I just breezed through.

[00:08:04] I was trying to breeze through cuz I don't want to get hung up on all that, cuz that's a whole another thing to play. I'm sure, there's a lot of people who've built servers before know that that's just like a whole another beast. But yeah, those are the two options that you have when it comes to sharing your node modules.

[00:08:17] It's either publishing it to npm, or you're deploying it somewhere. If you're not doing that, then I guess, you're just running it locally as a script.