Creating Public & Private Node Modules
Why Modules are important
Creating modules are important, especially when you want your code to be reusable. Node provides an architecture where you can write and reuse your code in the form of modules, through npm registry. The packages can be published publicly or privately according to your needs. Private modules also make you able to distribute your code to intended users, your team mates or private to yourself, accessible from anywhere.
Writing a Public Module
First create a folder MyModule anywhere. Then set the path to MyModule in cmd. Then you only need to create two files to create your first sample package.
- package.json
- index.js
now this can be the simplest module we can create, with just two files.
Creating package.json file
You do not need to explicitly create a package.json file. To create it, write in command prompt.
$npm init
This should ask you following questions:
name: (MyModule)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
Now you don't even need to answer these, few important questions are: version, description, entry point(which can be changed, by default it is index.js file) and author.
Answer those and skip the rest by pressing enter. This will create a package.json file for your module.
Optionally, you can directly create a package.json file, following the next steps:
- Create a text file, rename it to package.json
- Add the following code in it:
{
"name": "MyModule",
"version": "1.0.0",
"description": "My First Module",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "naeem shaikh",
"license": "MIT"
}
Now we have created the package.json file, we will create the index.js file, which is the entry point of our module.
The index.js File
Now this is our entry point to the module. We will create a simple to array to object converter. For ex:
we will convert this
['john','martin','scott']
to
{
"0": "john",
"1": "martin",
"2": "scott"
}
write following code in your index.js file.
exports.to_Object=function (arr) {
var obj = {};
for (var i = 0; i < arr.length; ++i)
obj[i] = arr[i];
return obj;
}
Done! we have created a module which exports a function to_Object, which will take an array as a parameter, and return converted object.
Publishing the Module
As we have completed the module, we can now publish it to npm registry. To publish your module, you must have a user on the npm registry. If you don't have one, create it with following command:
npm adduser
This will ask you the username, password to set and your email address. once done, you can login to npm using the following command:
npm login
This will store your credentials on client, and you would be now able to publish you module to npm registry.
Now the magic step, use this command
npm publish
This publish your package. To see if your package is actually puslished on npm or not? You can go to following url from your browser:
http://npmjs.com/package/<Package-Name>
Where?
Package-Name is the package name you just created and published.
Updating Your Package
You will need to update your package at some point with some additional feature. If you do, you can the change the version of your module, by using following command:
npm version <update-type>
Where?
update-type is one of the semantic versioning release types, patch, minor, or major. This command will change the version number in package.json.
Note that this will also add a tag with this release number to your git repository if you have one.
Additionally, you can also change the version number directly in your package.json file.
After updating the version number, you can npm publish again. You can then see the package updated at the same url: http://npmjs.com/package/<Package-Name>
Now, create a node project and change the current directory in _cmd _to your project. You can now install your module directly using
npm install Package-Name
using your own module in you node project is similar to how you use other modules such as express
var MyModule= require('MyModule');
Now you get access to your module, and the method defined in it as,
MyModule.to_Object(parameterArray)
write a simple node program, which will use our module.
var MyModule= require('MyModule');
var arr=['john','martin','scott'];
var result=MyModule.to_Object(arr)
console.log(result)
This is what you get in your console.
{
"0": "john",
"1": "martin",
"2": "scott"
}
Creating Private Modules
When you don't want to publish your module to anyone else except few, you can create a private module.
Publishing the private module is similar to publishing the public module, as discussed above, with just one difference, you publish them in your namespace or your team's namespace (with whom you wish to share your module), just by giving them a name in package.json:
{
"name": "@myuser/MyModule"
}
Publish them with npm publish , just like any other package, and you install them by namespace:
npm install @myuser/MyModule
Once installed, use them by requiring them by namespace, just like any package:
var MyModule= require('@myuser/MyModule');
Conclusion
Creating private or public modules is especially helpful to make you code reusable, distributive and make your code modular, so it's much more readable.
Thanks really useful, clear and concise…