Publishing to npm

Kent C. Dodds
InstructorKent C. Dodds
Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 5 years ago

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.

[00:01] 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.

[00:29] 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.00version.

[00:41] 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.

[00:58] 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.

[01:36] 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.

[02:03] 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.

[02:27] 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.

Alejandro Nanez
Alejandro Nanez
~ 9 years ago

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.

Kent C. Dodds
Kent C. Doddsinstructor
~ 9 years ago

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

Markdown supported.
Become a member to join the discussionEnroll Today