Thursday, November 26, 2015

How-to: develop a node module - part 1

In this course we've learned from day one, how to install node modules and use them in an app. A subject that might be of interest that this course does not bring up is, how does a node module work and how does one go on making one?

A node module is not really different from any component och "class" you make in an regular app. More or less any part of isolated code could be turned into a node module. What might slip one's mind is that whenever the require or import(depending on javascript version) is used, a module is imported for usage. A local one made by one self or a node module made by someone else there is not a big difference in how they're used or accessed.

So let's make a node_module!

I will presume the readers are familiar with git and starting new projects, also that node and npm is installed on the system. Therefore some of the following instructions are not in detail.
Also this will be written in classic javascript style(no TypeScript or ES2015), not that it makes a huge difference but still, now I've said it.
So now then:

In order to do this first step is to register an account at npmjs.com. The account is required for you to publish the module and making it available for everyone through "npm install"-command or the "package.json"-file.

Create a new git repository, I named mine npm-module-test for the sake of this article. Check the box for a readme-file if you want one. Then select "Node" from the gitignore-list at the bottom, this will just fill your gitignore-file with some standard ignore paths common for node projects.

Next, clone the repo to your work-environment and through the terminal/console(will call it terminal from now on) go to the folder where the repo was cloned. Now we can get started.

In the terminal simply type the command:
npm init

There will be a few questions to answer here about the project: its name, version, author etc. There are default settings here so one could complete this step by just pressing return through the entire setup. This will create a package.json-file with the information supplied in the setup. This file can of course be edited so nothing in there is final.

Take a look at the package.json file, there are two important fields in there. The first one is "name", this is the name of your module and this is the name that will be published in npm and used when installing the module. It is a good idea to only use lower case letters and dash to name your module.
For example "my-awsome-module".

The second is the field "main" that will have the value "index.js" if nothing else what chosen during the setup. This one is important because this is the file that will be run when someone uses the module, the entry file, the start file of the module. It's considered a good practice to put the source code in a separate folder from the root, normally named "src" so, create a folder in the root named "src", in that folder create a file named index.js. Now, in the config.json, change the value of "main" to:
{
    ...
    "main": "src/index.js"
    ...
}
Now we've redirect the entry point of the module to a more suitable location.

In this guide the module wont do anything useful at all, because that is not what it is about. So let's add some useless code to the module!

var UselessThing = function(){
    console.log('I am not that useful');
};
module.exports = UselessThing;

Save it and out module is done!

Now lets publish it on npm.
First we have to login to npm in the console using the command:
npm login

You will be asked to enter your username, password and then email

Using this command you can check your local settings to ensure your credentials was stored:
npm config ls

Now run this command to publish the module to npm:
npm publish

And it's done!
Well done! Your useless module is now public on npm and can be used by anyone! To take a look at it go to: http://npmjs.com/package/<package>
where <package> is the name of the module.

If you for any reason would want the code it's here:
https://github.com/afrxx09/npm-module-test
And the published version of the useless module can be found here:
https://www.npmjs.com/package/npm-useless-module-test

it can be installed by running:
npm install npm-useless-module-test

or added to your package json
"dependencies": {
    "npm-useless-module-test": "^1.0.0"
}

No comments:

Post a Comment