Tightening Node Project

Linting, CI, badges and pre-commit hooks for Node project.

I have lots of Nodejs projects, and most of them are in pretty good shape.

Maintaining these projects might appear a chore, but in reality it is quite easy. The key is to tighten the project right after implementing the functionality. The overall goal is to make the project easy to jump back to in the future.

Here are steps I take to tighten the module for long term maintenance:

  • Write unit tests, using a tool that provides code coverage. My favorite is my own gt. It has same syntax as QUnit, but has larger number of test types (external shells, performance) and runs natively under nodejs.

    • think of minimum coverage numbers for code and for unit test code. I would personally shoot for 75% - 90% test coverage for my source files, and 95% - 100% for the tests themselves (to make sure not too many tests are skipped at any point).
  • Setup travis-ci / Codeship.io / drone.io build. There is no excuse NOT to have CI system for an open source project.

  • Add badges to your README.md file to show build status, npm stats and out of date dependencies. For example, here are badges for one of my projects

NPM

Build status dependencies devdependencies

You can read more about badges in project status badges

  • Install grunt if it has not been installed already. Grunt is a great JavaScript build tool, extremely flexible and it runs all the tasks described in the next steps.

  • Install grunt-complexity and setup maximum complexity limits in your Gruntfile.js. I believe for long term maintenance this is an extremely important step. Try to refactor code to have lower complexity while the code is still fresh in your memory.

  • Install grunt-nice-package with npm install grunt-nice-package --save-dev and add nice-package task to your pipeline:

Gruntfile.js
1
2
3
4
'nice-package': {
all: {}
}
runt.registerTask('default', ['nice-package', ...]);

grunt-nice-package is a very lightweight sanity checker for your package.json, warning you if there are any missing fields. Adding the missing fields makes package reuse easier for others.

  • Add grunt-contrib-jshint to catch simple JavaScript errors through static source code analysis. You can also add jsonlint and coffeelint if needed.

  • If your project has both package.json and bower.json, add grunt-npm2bower-sync to sync main properties from package.json to bower.json

  • Install git pre-commit and pre-push hooks via pre-git. Pre-git keeps master branch from accidentally breaking locally and remotely. Recently, I started using the following pre-push hook commands:

    "pre-push": [ "rm -rf node_modules", "npm install", "npm test" ]

Hope this list helps you improve and maintain your nodejs modules.