Next up, I want to publish the package that we just created called hello. And by publishing it, I'll make it available to other people who want to use it from around the world. And I also then don't have to link to it locally in order to use it. I can just install it directly from npm.
So to install the module to npm, I can use the publish command. And if I type npm and then publish and press Enter, I'm going to get an error. And the reason for the error is it's saying I need a paid account to perform this action. And that's because, by default, packages that are namespaced, so namespace with this login name and prefix here, are going to be private. And that's because npm doesn't want you to accidentally publish a private package and make it public.
So what we need to do is to tell npm that this is a public package. And public packages on npm are free, and private ones are going to cost money. So that is how the npm business model works. So what we can do is type npm publish and provide this access flag and set it to public. And that will tell npm to publish this package and to make it public to the world. And that doesn't cost anything.
That process might take a couple of seconds. But then it will tell you with this plus sign that this package, cmather hello at version 1.0.0 has been published to npm. Let's go check it out. Go ahead and log in to the npm site and click the link that is your login name. And you should see the package that you just published in this list here.
So here's the cmather hello package version 1. And I can click on that, and it'll show me the information that other people will see about the package. For example, the person who published it, which is me, the way that we can install it, any collaborators, keywords, the license, and so on.
Now, we didn't add a readme. So it looks as if there's no readme found here. But that's OK. So this is also telling me that this is public. And so anyone in the world can find this package, install it, and use it.
All right. Back in our root folder, change into the app directory. And the way that we can install this npm package is by typing npm install and then the name of the package along with any namespaces. So I'll say @cmather/hello. That process might take a couple seconds. But when it completes, you'll see that it's put the package into a folder inside of the Node module's directory.
So here's our Node module's directory. And if we change into it, notice we still have the @cmather folder. And if we change into that, previously we had a symlink here that was symlinking to our project on the local machine. But now it's just a regular folder. And so this folder has been copied from npm directly into the Node module's folder of my project. And if we navigate into that folder, you'll see it's just the source code for this version of the package.
One thing that we've already talked about in the introduction to Node.js class is that generally you don't check in this Node module's folder into Git because the folder can get quite large. Instead we just create a package JSON file right in the application project folder itself, and that'll describe which Node modules should we install when we type npm install. So we can use the same init command that we used previously to initialize a package JSON file right here in our application folder.
So I'll just press Enter a bunch of times until we get down to the bottom. And if we look at our project in Vim and check out the package JSON file, notice that there's this dependencies property right here, and we can add dependencies inside of this object.
I'm going to quit out of this and show you how we can install dependencies very quickly into that file. So let's install the cmather hello package again, but this time, type dash dash save. And what that flag will do is automatically install the package but also add it to our package.json file. So notice now we have this new dependency, and it's saying that the package by the key is @cmather hello, and its value is the version of the package that we want.
And this little caret here in front says that we want the version of this package that is 1 dot whatever. So if a new version of the package comes out that's 1.5, that's fine, 1.6 and so on, all the way up until 2.0. So it'll hold off on installing 2.0. It'll stay with the current major version of the package, which is 1.
Back in the console now, if I type npm install, it will install all the packages that are specified in that package.json file. So I can check the package JSON file into the GitHub repository. And then any collaborators on the project can just type npm install, and all the required Node modules will be put into the Node module's folder automatically.