Distributing your npm package as an executable

Here are simple steps to setup your first npm package. You do not need to publish directly to npm repository instead you can push to git.
First thing you gotta do is - setup github.

a) Create a new repository and clone it to your local drive

b) Execute 'npm init'.

c) We will also use index.js as the default entry point. This means  we will create a file called "index.js" and it will host our modules, which might looks something like this. (Please note : You will need to add 

#! /usr/bin/env node 

as shown below, otherwise node will not be available. :-



d) Next, we need to edit our package.json and add the following


"preferGlobal": false,
"bin": {  "npmcalc": "./bin/index.js"

This additional properties basically tells node that we are going to get an executable called npmcalc and source located in a file called index.js under bin folder.

If you set 'preferGlobal' : true - you need to install npmcalc with a -g option, otherwise you might not be able to find the npmcalc itself.



It would look something as follows :-




e) Please git commit your code and push to master branch.

git commit

git push origin master


f) Next, lets try out by installing it from github.

npm install git://github.com/appcoreopc/npmcalc.git


g) From the command line, execute

npmcalc 10 10

Total result = 20





Comments

Popular posts from this blog

OpenCover code coverage for .Net Core

Android Programmatically apply style to your view

Using Custom DLL with IronPython / Scripts