Authoring node modules
Tags: node, javascript, npm
Published: 2018-08-31 03:28:18

---
title: Authoring node modules
published: true
description: 
tags: node, javascript, npm
---

An old [post](http://h3manth.com/new/blog/2015/authoring-node-modules/) of [mine](https://h3manth.com) a friend suggested to re-post it here.

So far I have authored/contributed around [200+](https://www.npmjs.com/~hemanth) modules on `npm` and they get around `1.5M` downloads per month, which is a decent figure; as a part of this I have learnt a few things and I would like to share the same in this post.

__Todo before writing a module:__

* Search npm and look for an already existing module that solves your issue: you could do a `npm search <module>` or search from the [site](https://www.npmjs.com/)

* If you find one such good module, do read its code and see if you can improve something, it might be performance, logic or test fixes.

* If you find a module and it does not do exactly what you are looking for, then send a `PR` with the changes you wish.


__Writing a neat package:__

* If you are one of those smart lazy devs, you might want to use [generator-node](https://github.com/yeoman/generator-node). If that is too heavy for you then checkout [generator-nm](https://github.com/sindresorhus/generator-nm)

* If you do not want `generators` you can stick with `npm init` and create the required dir structure manually.

* Make sure your `package.json` is valid, maybe you would need [package-json-validator](https://www.npmjs.com/package/package-json-validator)

* If you are new to `npm` you can use [validate-npm-package-name](https://www.npmjs.com/package/validate-npm-package-name) to check if your package name is a valid npm package name.

* It would be good if you make sure that your `package.json` has a `files`  attribute which in turn has a [main](https://docs.npmjs.com/files/package.json#main) attribute in it.

* Make sure you have test cases in place and also integrate it with continuous build service, like [travis](https://travis-ci.org/).

* Use badges like: [nodei](https://nodei.co/), [david](https://david-dm.org/) et.al

* Make sure your `README.md` looks good, as it has info about how to install your module, what it does, API docs and a gif, logo is a plus.

* Take care while releasing package, using tools like [release-it](https://www.npmjs.com/package/release-it)


__Getting to the top:__

There are many ways to showcase your module so that people will find it and hopefully use it but here are a few good ways to create a useful package:

* Dig into GitHub and find a few trending projects, read the source, find code that can be extracted into a module, make a module and send a PR with it, most of time it gets accepted as modularity always wins.

* Dig into the [most dependent](https://www.npmjs.com/browse/depended) modules of node and repeat the above steps.

* If the above did not work in your favour, make your own light weight module that just does a small part of the larger module.


P.S: It's not just about getting to the top, rather it's about doing one thing and doing it the best way.

__Further reading:__

* I strongly believe in writing one-line node modules, but I wanted to hear the same from [@sindresorhus](https://twitter.com/sindresorhus) so I asked him a [question](https://github.com/sindresorhus/ama/issues/10).

* Me and few of my friends ([Stoeffel](http://stoeffel.github.io/) & [Tomekwi](https://github.com/tomekwi) ) together have put up [1-liners](https://github.com/1-liners/1-liners) and also checkout [npm-scripts](https://github.com/npm-scripts/scripts)

* Apart from what I had mentioned for `readme.md` have a look at this [template](https://gist.github.com/jxson/1784669).