ConsoleFancy – my first npm module

First off, I’m not really sure if it’s a module, or a package, or a repo or what. Maybe it’s all three. I’ll find out one day.

More importantly I’ve added to npm! You can install a package that I’ve written right now – it was ridiculously easy to do.

vi test.js

// create the following in test.js
// var ConsoleFancy = require('console-fancy'),
// consoleFancy = new ConsoleFancy();
// consoleFancy.header('hello');

npm install console-fancy
node test.js

After all that hard work you should see hello wrapped in the default top level borders – currently a lot of # symbols. You can switch it to consoleFancy.header('hello',2); for the second level header or when you init the object you can pass it more levels and specify the border, padding and edges. I’ll expand the docs to show how to do that when I get some more time.

It all came about as I was writing a Node.js app to download and process Apache log files from my Load Balancer, when I got frustrated at the amount of times I’d written console.log(). It was all blending into one output and was hard to read what was going on. I wanted to break the output up so I could clearly see major points in my output.

So I wrote a function that I could pass my log message to, as well as a level heading, like an <h1> or <h2>. This worked fine but I wanted to be able to re-use it and share it with my colleagues.

I created a new script and included that with require('./ConsoleFancy.js') and then thought “How do I get this into npm? Can I add this, and just install it like all the other packages?”

Well the answer is yes. It’s incredibly easy. It’s all explained here:
https://docs.npmjs.com/getting-started/publishing-npm-packages

In short: Once I’d signed up at npmjs.com, as I’d already created my node module/package thing, I just ran the following to get it published:

// this created the package.json for me - it asks you questions, you answer them
npm init

// log into the npm site, so it publishes to your account
npm login

// then you publish it... if there's any problems it errors and you can try again
npm publish

The process gets most of the information it needs from the package.json. When I ran the init command it asked me what the Github repo was, so I actually cancelled the process, created a repo, pushed it all live and then started again.

Adding the Git repo means it’s linked from the npmjs site, so other people can contribute, submit pull requests, issues etc.

When you update the module, once you’ve made you changes, commited your code, you run the following command:

// patch can be replaced with minor or major
// https://docs.npmjs.com/getting-started/semantic-versioning
npm version patch

This command is brilliant, it does 2 things:

  1. Updates the package.json file, incrementing the version number based on your patch/minor/major keyword
  2. Creates a Git tag for your repo so you can get back to this release commit – the tag matches the version number

Once you’ve created your new version you just run npm publish again and it not only pushes it npmjs.com, but also pushes the new commit and tag to Github!

It’s also worth making a README.md file file (or whatever format you like for your README) as this gets pulled into your npmjs page – without it you just get a blank page with the project name on it.

So if you find yourself writing utilities and tools for Node.js, and you’re considering sharing them then go ahead and do it. It’s so simple, it’ll make your life easier in your next project, you’ll be helping others and if it picks up you can get some help to improve it.

Just don’t forget to check if someone else has already written it… I did look, but I bet someone’s already written a ConsoleFancier.

Project links:


Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s