So, you want to publish your first NPM module?

You have just written some badass code.
You want to share it with the world.
You think it will make the perfect new NPM module.

BUT… you have no idea what to do to make that happen.

No worries!!

It’s simple, fun and I am here to walk you through the process.

Lets start with the code you have written (this post assumes it’s written in NodeJS).

A sure fire way to get users digging your new module is making sure that once they require it into their project, that it is easy to use.

A couple of ways I have found for doing this are simple.

First, if your code is a cool function that you want to give to the user:

module.exports = function(data) {
// Your awesome secret sauce here

Now your end user can use your cool new module like this:

var coolNewModule = require('yourModule')

Pretty simple, right?

Now I know what you’re saying, “But Kevin, what if I have a bunch of cool functions I want to give the user?” Great question!

Here’s a great and simple way to do that:

function one (data) {
// whoa, awesome function dude!

function two (data) {
// Holy cow, even cooler function!

module.exports = {
one: one,
two: two

What this is doing is returning an object where the value of each property is actually set to the cool function you just wrote, also affectionately referred to as object methods 😜.

Your end users would now be able to use the functions like this:

var coolNewModule = require('yourModule')

“But Kevin, what If I just want to deliver the end user with module that simply takes an input and delivers a transformed output ?”

That’s a great question and it’s also really easy!

module.exports = function(data) {
// your awesome code that transforms stuff...
return {
//...will ultimately return this awesome newly formatted object

Now you end user can use it like this:

var coolNewModule = require('yourModule')(data)

How cool is all that!

“Kevin, how do I actually get it onto NPM after writing all that awesome code”

A few prerequisites:
1.) Make sure you have a ‘package.json’ set up correctly in your project (This shows more about package.json files clickMe)
2.) Make sure you have a NPM user
3.) Tests aren’t a bad thing to have in your project either 😛

**For this part, make sure you have already created a user on NPM

A simple 3 step process will get your cool new module published:
1.) Inside you terminal, CD into your awesome new project and run ‘npm adduser‘ and follow the prompts on the screen.
2.) Ensure things have been set up correctly by running ‘npm config ls‘ (you should see all your configs printed out)
3.) This is the one you’ve worked so hard to get to… run, ‘npm publish

And just like that you are now an open source contributor!!

Give yourself a pat on the back and keep contributing, because the world needs more people like you!

If you would like some video guidance on those 3 steps, be sure to check out this awesome video that NPM has already put together VIDEO


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s