Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 829 of the free egghead.io lessons, plus get JavaScript content delivered directly to your inbox!



Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.



Publishing to npm

2:36 JavaScript lesson by

Putting your code up on GitHub is helpful, but publishing your library to npm makes it even easier for people to use your library. In this lesson, you'll learn how to publish your library and validate that it published correctly.


egghead.io comment guidelines

Avatar
egghead.io

Putting your code up on GitHub is helpful, but publishing your library to npm makes it even easier for people to use your library. In this lesson, you'll learn how to publish your library and validate that it published correctly.

Avatar
Alejandro

If you have issues publishing your package is because there's already a starwars-names package published, you should named it like yourname-startwars-names for example.

Avatar
Kent C.

Thanks for bringing that up @Alejandro. I would also recommend that if people are just following along, that instead of publishing a redundant starwars-names package, that you publish it as a scoped module: https://docs.npmjs.com/misc/scope

In reply to Alejandro

Now we're finally ready to publish our node module. We've already configured everything locally with NPM in a previous lesson, as well as in our package JSON with this metadata. All that really needs to happen is to simply run npm publish. This will go out to NPM, register our module in the registry, and then create the starwars-names module.

Now we can run npm info starwars-names. You'll notice this looks similar to our package JSON. This allows us to install our 1.0.0 version.

Let's go ahead and test this out a little bit. Let's just change our directory to the desktop, and then we'll run npm install starwars-names. This will install our module and our module's dependencies.

Now, we can create a file that uses our Star Wars names, so we'll say vim index.js, and we'll insert a starwars-names require starwars-names, and then we'll console.log starwars-names.all. Then we'll console.log starwars-names.random, and we'll do that a few times.

Now if we say node index.js, we get all those printed, and then our random items printed as well. If we run it again, the random ones are different every time. So it looks like we successfully published our first package to NPM. If we go to the NPM registry, we can shortcut to our module name with npm.im/starwars-names, and this will take us to our registry item.

Here we have starwars-names, get random Star Wars names. Our readme could use a little bit of help, but it has a link to our starwars-names. It has our license here. It lists me as a collaborator, and other information about our module, such as our dependencies. That is publishing your first module to NPM.

In review, all that you need to do is run npm publish. As long as you've configured your NPM with your user, then you should be all set.

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?