introduction to npm and building cli tools with node.js

17

Click here to load reader

Upload: suroor-wijdan

Post on 01-Jul-2015

593 views

Category:

Technology


2 download

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

Page 1: Introduction to NPM and building CLI Tools with Node.js

Introduction to

Introduction to

By Suroor Wijdan

Page 2: Introduction to NPM and building CLI Tools with Node.js

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

Page 3: Introduction to NPM and building CLI Tools with Node.js

Introduction to

What is NPM?First Time Using NPM

Page 4: Introduction to NPM and building CLI Tools with Node.js

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!

Page 5: Introduction to NPM and building CLI Tools with Node.js

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}

Page 6: Introduction to NPM and building CLI Tools with Node.js

Introduction to

Where to find Packages?

https://npmjs.org

Page 7: Introduction to NPM and building CLI Tools with Node.js

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.

Page 8: Introduction to NPM and building CLI Tools with Node.js

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.

Page 9: Introduction to NPM and building CLI Tools with Node.js

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.

Page 10: Introduction to NPM and building CLI Tools with Node.js

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!

Page 11: Introduction to NPM and building CLI Tools with Node.js

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.

Page 12: Introduction to NPM and building CLI Tools with Node.js

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’]

Page 13: Introduction to NPM and building CLI Tools with Node.js

Introduction to

Command LineSo its better to slice the array and store the arguments in another variable:

var argumentsPassed = process.argv.slice(2);

Page 14: Introduction to NPM and building CLI Tools with Node.js

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

Page 15: Introduction to NPM and building CLI Tools with Node.js

Introduction to

Questions?

Page 16: Introduction to NPM and building CLI Tools with Node.js

Introduction to

/THANKS/ig

Page 17: Introduction to NPM and building CLI Tools with Node.js

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