Don’t be put off however if you do not have that much experience as most of the examples in the book are not that hard to follow.
Contents1. Book cover2. Who is this book for?3. Contents4. Npmjs.org description5. Statistic of how many downloads6. Installing npm7. Chapter One8. Pablo Picasso quote9. How to install a package
10. How to globally install a package11. How to uninstall a package12. Creating package.json and saving packages to it13. Structure of package.json14. Install all listed packages15. How to require packages16. Chapter two17. How to update packages18. Choosing which version19. Checking what to update20. Chapter Three21. Jquery.extend22. Underscore23. CoffeeScript
24. Colors25. Chapter Four26. Registering up 27. Using npm config ls28. How to structure a custom
package29. Publishing a package30. The process of creating31. Updating custom packages32. Chapter Five33. List of all npm commands34. Useful npm commands - P135. Useful npm commands - P236. Npm explore37. Chapter Five38. Npm.load()39. Npm.commands40. Using packages with npm41. Image of finished script42. Chapter Seven43. Statistics of npm44. What you have learnt45. Credits
1. Go to npmjs.org,2. Search for the package name you want, for
e.g: “underscore”.3. Then simply type on the command line:
What if i need it installed everywhere?
Add -g to then end of your npm install to make the package globally available!
npm uninstallJust use npm uninstall just like npm install to remove a package. To remove a global package just add -g the end of npm uninstall.
npm initThis command creates a file called ‘package.json’ this holds a list of all your packages installed.
npm install --save <p_name> This parameter added to the end of a npm install adds the package name to the package.json. You will need to run npm init before using --save.
npm init and --save
npm uninstall --save <p_name> This removes the package from the ‘package.json’.
package.jsonWhere our packages
What version of
the package do we need
Manually added packages to package.json?
Just use npm install without specifying a package name to install all of them!
Where to use it?Require is placed inside files that are to be run by Node.js and that “require” a package to work. Require is strictly for the server side code unless you are using Browserify!
How to use it?First we store our required modules in a variable, then we call the require function with the name of our package in the brackets.
2.Updating and checking● npm update● npm outdated
If you want to update all the packages in package.json simply type npm update. However you can also just add a package name to update a single package.
For global packages you just have to reinstall the package globally.
StableThis is the safest
option as this has no further
LegacyYou might want this if you need
to support older libraries
EdgeThis is the latest version and the
most unstable as it is new and still being developed.
What kind of version of a package do you need?
Use npm outdated to check whether any packages need to be updated.
3.Libraries to check out
● jQuery.extend● Underscore● CoffeeScript● Colors
jQuery.extendAlthough you may not be able to select DOM elements, jQuery’s useful $.extend() method can easily help to simplify inheritance on the server side.
npm install jquery.extend
Extending objects for inheritance.
It only offers one method.
UnderscoreThe npm package explains this perfectly.
npm install underscore Offers a full library of useful functions.
Do you really need all of them?
npm install coffee-script
More readable and cleaner than JS.
Debugging is a pain.
ColorsAdd coloured text to your Node.js console.
npm install colors
YOU GET COLOURS!
Nothing colours rule.
4.Publishing your own package
● npm adduser● npm publish● npm unpublish
To add a package we need to create an account. We can do this easily with npm adduser.
You should see the fields outputted when ran:
Fill out the fields and continue, you should now be
use npm config ls to check whether your credentials are
saved on your system.
Simple package - Default file name “index.js”
Requiring it after publishing
npm init Run npm init to create a package.json file, remember to add any dependencies you installed as you made the package.
PublishingAfter setting up the package.json simply type npm publish ./ this will publish the package in the current directory to npmjs.org.
npm adduser npm init npm publish
The process is simple
Updating the package
Versionnpm will reject an update with the same version code as the published package, so remember to change the version code.
npm publish againAs your credentials are saved on your system you do not need to relogin, so to publish the update simply run the npm publish command.
npm editThis opens the selected package in your default editor, after saving npm rebuilds the package.
npm docsThis command tries to guess at the likely location of a package's documentation URL, and then tries to open it in the browser.
npm lsPrints out all the packages installed as well as their dependencies in a tree like structure.
● npm edit <p_name> ● npm docs ● npm ls
npm explore <p_name>
This command will open a subdirectory in the command line to the package in the node_modules folder. Super useful!
6.Programming with npm● npm.commands● npm.load
Setting up our script
We will call our file “test.js”, then require the “npm” module.
First we must load our environment, we can do this via npm.load(). The method takes 2 parameters a configuration object and a callback when npm is ready.
Inside npm.load’s callback we can use npm.commands to execute some command line methods. For example we will use the npm ls method to list out all our packages.
Using other packages in conjunction
We will use the prompt package to allow user input. To install it run npm install prompt, then require it in our test.js.
We use prompt.start() to start prompting, then we run prompt.get() and pass in an array with our field names (the use of “_” will result in a space in the command line) and a callback with the parameters err and result.
Our results are stored in the result object, to access them use the same name in your array as a property. For e.g: [“package_name”] will equal results.package_name .Then we substitute our field (“package_name”) into npm.commands.explore.
Full size image next page.
test.js with the prompt package integrated (top), program running in cmd (bottom).41
170, 699That’s a lot of packages
100%Guaranteed to speed up your workflow
Around 30k installs a dayAnd a lot of updates
Let’s review what you learnt
Install and uninstallYou learnt how to use npm install, npm uninstall, the --save and -g param and the structure of package.json.
Updating and checkingYou learnt how to update packages locally and globally as well as how to update all packages. You also learnt how to check if updates are needed using npm outdated.
Some librariesYou learnt about the positives and negatives of using certain packages and how to install them.
Publishing your own packageYou learnt how to write a basic package and how to upload it to the npm registry. You also learnt how to update the package and what requirements must be met before updating.
Extra npm commandsYou learnt some useful npm commands to help speed up your workflow.
Programming with npmYou learnt how to require npm and use it in an example project to explore files.
Special thanks to all the people who made and released these awesome resources for free:
BlurbLearn how to use npm to install packages and update them. Delve deeper into more command line commands to help speed up workflow and finally use npm in your scripts to utilise npm’s additional features.