Creating and registering a Node Package Module
Full stack 3.2 : FX-price-service Node Package Module
Following on from 3.1 we are now looking to de couple the fx-price service from our main application.
This is in order to have a loosley coupled dependancy that we can manage through the npm repository.
Setting up and defining a node package
The steps involved in publishing a node package are the following.
- Preparing a package
- Writing a specification
- Testing a package
- Publishing a package
Preparing an npm package
We will be setting up our package using the standard dirctory structure for an npm package.
/ bin
/ docs
/ example
/ lib
/ test
bin : Command line scripts
docs : Documentation
example : Demo of modules use
lid : Core application functionality
test : unit and integration tests
Writing a specification
A npm package specification is represented by our package.json file.
To produce a package.json file run the following command in the directory of your package.
npm init
This will ask you a bunch of questions to set up your defaults — after answering these questions and making some changes the gch-truefx-pricefeed package.json looks like the following.
{
“name”: “gch-truefx-pricefeed”,
“main”: “index”,
“version”: “0.0.1",
“description”: “FX price service “,
“homepage”: “https://github.com/Goochie/gch-truefx-pricefeed",
“keywords”: [ “websocket”,“socket”,“FX”,“socket.io”],
“dependencies”: {
“socket.io”: “~0.9.16",
“cheerio”: “^0.17.0",
“request”: “^2.36.0"
},
"repository": {
"type": "git",
"url": "https://github.com/Goochie/gch-truefx-pricefeed"
}
}
All of the above are very much self explanatory the one property that seems a little quirky is …
“main”: “index”,
This property is the link to the main source that represents the module.
A few things to note is that its is common convention to provide an index file that links to your main source held in lib.
index.js
/**
* Author : Bill Gooch
* MIT Licensed
*/module.exports = require(‘./lib/fxprice-srv’);
The second thing to note is that node will look up index.js from index.
Testing a package
In this context testing is not unit / integration testing it is testing the installation process of your package locally, which enables you to test that it does what it says on the tin.
Node provides a useful method, which enables you to install a package locally.
Given that you are in the root directory of your package, you have to run the following command.
npm link
This command will install the package locally but treat it as a global package.
You can then install it in a seperate test project by running the following command in your project.
npm link gch-truefx-pricefeed
… which equates to
npm link mypackage
Once you have finished testing your package simply run the following command to unlink your package.
npm unlink gch-truefx-pricefeed
Publishing a package
An NPM publishing account is required before publishing a package, go to the NPM site to create one.
After you have created an account you will need to authenticate your machine and this is achieved by running the following command.
npm adduser
You will then be prompted to add your credentials.
Once this has been done you then simply run the following command from your package’s directory to publish to the repository.
npm publish
Conclusion
As you can the gch-truefx-pricefeed has now been successfuly deployed to NPM, which enable us to treat it as decoupled dependency for any further projects.