introduction to npm and building cli tools with node.js
DESCRIPTION
In this talk, we talked about NPM. Why is it so powerful and verticals where it is being used apart from Node.js. Also we go through the basic packages and how can we build a simple CLI tool with Node.js and publish it on NPM.TRANSCRIPT
Introduction to
Introduction to
By Suroor Wijdan
Introduction to
Agenda● What is NPM?● Understanding package.json● Where to find Packages?● Installing Packages● Writing your own Packages● Command Line● Publishing to NPM● Questions & Exercises
Introduction to
What is NPM?First Time Using NPM
Introduction to
What is NPM?● A Node Package Manager● No other Package Manager comes closer● Is also used by developers who don’t use Node.js at all● Created by Isaac Z. Schlueter● Breeze to use● Recently made into a company, NPM Inc. by Isaac● Free and Open Source!
Introduction to
Understanding package.json● It is a valid JSON object ● name and version fields are required, the combination
makes a unique identifier for the package● Some used fields in package.json
○ description○ keywords○ homepage○ bugs○ license○ author & contributors○ main○ bin○ dependencies○ scripts - {start, preinstall}
Introduction to
Installing Packages● npm comes along with node.js● to install a package run:
○ npm install <package-name>○ npm install <tarball file>○ npm install <tarball url>
● install a package globally ○ npm install -g <package-name>
Note: Global installation makes the package available globally irrespective of the directory you installed it from.
Introduction to
Writing your own packages● Install Grunt , the javascript task runner
○ npm install -g grunt-cli● Run command :
○ grunt-init node● Answer few questions and you are ready with a nice
package.json file and main .js file for your package.
Introduction to
Writing your own packagesThe generated folder structure should look something like this:
Write the code in the .js file created, you can also create many more files as per your need.
Introduction to
Writing your own packagesSome guidelines to keep in mind when writing new packages:
● Don’t reinvent the wheel● Try to contribute to other packages whenever possible● If you are writing a utility package, then multiple exports should be used● If your package needs to maintain a state then its good to export a
constructor● Most Important thing, KEEP IT COOL!
Introduction to
Command LineTo be able to use your package through command line, you need to specify ‘bin’ in package.json as follows:
"bin": {
"nodeart": "lib/nodeArt.js"
}
Also add this #! /usr/bin/env node at the top of “lib/nodeArt.js” file. This tells how the file should be executed.
As above, ‘nodeart’ will be the command available to you when you install the package on your machine.
Introduction to
Command LineYou can receive the arguments passed from command line in process.argv array. First two values in the array are pre-defined :
[ 'node', '/opt/node-v0.10.5-linux-x64/bin/nodeArt']
The actual arguments being passed will be available at indexes greater than 1. So if you run nodeart ‘suroor wijdan’, the process.argv array will be:
[ 'node', '/opt/node-v0.10.5-linux-x64/bin/nodeArt', ‘suroor wijdan’]
Introduction to
Command LineSo its better to slice the array and store the arguments in another variable:
var argumentsPassed = process.argv.slice(2);
Introduction to
Publishing to NPMTo publish on NPM, you just need to have an account on it. To make an account run the following command:
Once you have the account ready, just run the below command from project root to publish your module:
And thats all, everything else will be taken care of by NPM registry.
npm adduser //will ask for username, email and password
npm publish
Introduction to
Questions?
Introduction to
/THANKS/ig
Introduction to
References
1. NPM Logo - https://npmjs.org2. https://npmjs.org/doc/3. Image Credit - http://nodejsreactions.tumblr.com/post/64781824365/first-time-using-npm4. Grunt Logo - http://gruntjs.com