SlideShare a Scribd company logo
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
Where to find Packages?
https://npmjs.org
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 packages
The 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 packages
Some 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 Line
To 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 Line
You 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 Line
So its better to slice the array and store the arguments in another variable:
var argumentsPassed = process.argv.slice(2);
Introduction to
Publishing to NPM
To 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.org
2. https://npmjs.org/doc/
3. Image Credit - http://nodejsreactions.tumblr.com/post/64781824365/first-time-using-npm
4. Grunt Logo - http://gruntjs.com

More Related Content

Introduction to NPM and building CLI Tools with Node.js

  • 2. 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
  • 3. Introduction to What is NPM? First Time Using NPM
  • 4. 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!
  • 5. 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}
  • 6. Introduction to Where to find Packages? https://npmjs.org
  • 7. 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.
  • 8. 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.
  • 9. Introduction to Writing your own packages The 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.
  • 10. Introduction to Writing your own packages Some 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!
  • 11. Introduction to Command Line To 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.
  • 12. Introduction to Command Line You 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’]
  • 13. Introduction to Command Line So its better to slice the array and store the arguments in another variable: var argumentsPassed = process.argv.slice(2);
  • 14. Introduction to Publishing to NPM To 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
  • 17. Introduction to References 1. NPM Logo - https://npmjs.org 2. https://npmjs.org/doc/ 3. Image Credit - http://nodejsreactions.tumblr.com/post/64781824365/first-time-using-npm 4. Grunt Logo - http://gruntjs.com