Own Node.js project installed globally on own system

Background

I am very new to Node.js so please don't hate..

I found NPM very useful because I can install Node.js packages globally and then use them like standalone, available-on-path apps.

This does work on Windows, which really suprises me.

For instance I installed UglifyJS this way, via npm install -g uglifyjs and now I can run it from anywhere in my system, from the console via uglifyjs <rest of command> (not node uglifyjs .. or sth else).

I'd like to create my own stand-alone Node.js application. How do I get starded? I am asking here because most tutorials only cover how to write a simple script and then run it va node (which I already covered)


My current config

package.json:

{
    "name": "hash",
    "version": "1.0.0",
    "author": "Kiel V.",
    "engines": [
        "node >= 0.8.0"
    ],
    "main": "hash.js",
    "dependencies": {
        "commander" : "1.2.0"
    },
    "scripts": {
        "start": "node hash.js"
    }
}

hash.js:

var crypto = require('crypto'),
    commander = require('commander');

/* For use as a library */
function hash(algorithm, str) {
    return crypto.createHash(algorithm).update(str).digest('hex');
}
exports.hash = hash;


/* For use as a stand-alone app */
commander
    .version('1.0.0')
    .usage('[options] <plain ...>')
    .option('-a, --algorithm [algorithm]', 'Hash algorithm', 'md5')
    .parse(process.argv);

commander.args.forEach(function(plain){
    console.log( plain + ' -> ' + hash(commander.algorithm, plain) );
});

Question:

Suppose I have only these two files in node-hash directory. How do I install this project, so that later I can run it in cmd.exe via hash -a md5 plaintext just like coffescript, jslint etc. installs ?

You have to add some code into package.json and hash.js, then you can run this command to install the package from local folder.

npm install -g ./node-hash

package.json

{
    "name": "hash",
    "version": "1.0.0",
    "author": "Kiel V.",
    "engines": [
        "node >= 0.8.0"
    ],
    "bin": {
        "hash": "hash.js"
    },
    "main": "hash.js",
    "dependencies": {
        "commander" : "1.2.0"
    },
    "scripts": {
        "start": "node hash.js"
    }
}

hash.js

#!/usr/bin/env node

var crypto = require('crypto'),
    commander = require('commander');

/* For use as a library */
function hash(algorithm, str) {
    return crypto.createHash(algorithm).update(str).digest('hex');
}
exports.hash = hash;


/* For use as a stand-alone app */
commander
    .version('1.0.0')
    .usage('[options] <plain ...>')
    .option('-a, --algorithm [algorithm]', 'Hash algorithm', 'md5')
    .parse(process.argv);

commander.args.forEach(function(plain){
    console.log( plain + ' -> ' + hash(commander.algorithm, plain) );
});