Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Publishing to npm


    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.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    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.00version.

    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.