@bytesnz

Jack Farley, Web Application Engineer

Developing NPM Modules

I have been developing some NPM modules in the past little while and thought I would jot some things about it down. I will keep on adding things as time goes by.

Update

A better way to develop multiple libraries at once is to use npm link. For example, the json_crud library depends on the skemer library. To use the version of skemer I am currently developing in json_crud, I can run:

1
2
3
4
$ cd skemer
$ npm link . # This will create a link to this instance of the skemer module
$ cd ../json_crud
$ npm link skemer # This will use the link (link to it in the node_modules folder) created above

Unfortunately both this way and the way mentioned below causes issues with the combination of Gulp, Jasmine and Istanbul meaning that the tests don’t run when the files are updated (if they are watched by Gulp).

Development folder

I have started developing my modules inside of a node_modules folder, eg:

1
2
3
4
node_modules/
skemer/
moltendb/
json_crud/

This way if you want to use one of the modules in one of the other modules you are developing and want to use code not yet published to npm, you can simply delete the npm-installed copy in the node_modules folder of the module you are developing and hey presto, require('<module>'), will include your development version thanks to node’s module resolution. Just don’t forget to mark the package as a dependency in your package.json file.

Development tools

Here is a list of my “standard” development tools for NodeJS:

  • gulp
  • eslint
  • jasmine
  • istanbul (though I problems with this at moment where if a jasmine test fail, istanbul doesn’t run [because the stream doesn’t get closed by jasmine])
  • coveralls
  • jsdoc - not the best (things like @link are included in the markdown output, which is annoying), but will do for now.

Testing

Testing. Everyone should be testing their modules. In my Skemer package, I have used jasmine for testing. I currently have two levels of testing - testing that is run on the files as they sit in the src folder, and a “production” test that tests the files as they sit where they would in the npm module. The problem with my “production” tests at the moment though is that the tests still run with all the development stuff around it, meaning that if I put a module down as a dev dependency instead of just a dependency, my tests are going to detect it. I am in the process of creating a npm module (package-test) to automate the copying of the module files in a “production environment” folder for a bit more realistic testing.

Including tests in your NPM module

I don’t include tests in my modules (using a .npmignore file). I think that modules should contain only the files they need to function, so that when you are installing them or packages you are using, you save time and bandwidth not having to download a whole bunch of extra files that you will probably never use (I have never ran tests on modules I have used). If people do want to run tests, they can grab the module from github or whereever you’re developing it (because you share in the opensource community).