Upload
kameron-tanseli
View
118
Download
0
Embed Size (px)
NPM THE GUIDEBy Kameron Tanseli @kamerontanseli
1
Who is this book for?This book accommodates for beginners however the ideal reader is one who has a working knowledge of the command line and JavaScript as well as Node.js installed.
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.
2
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
3
“npm makes it easy for JavaScript developers to share and reuse code, and it makes it easy to update the code that you're sharing” - npmjs.org
4
1,908,826,857Total number of installs last month
at the time of writing this.
5
Installing and updating npm
Npm comes with the installation of Node.js.
To update npm simple type
6
1.Using npm
● npm install● --save● require( );
7
“Good artists copy; great
artists steal - Pablo Picasso.
8
npm install
Here are 3 easy steps to installing a package:
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:
9
What if i need it installed everywhere?
Add -g to then end of your npm install to make the package globally available!
10
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.
11
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’.
12
package.jsonWhere our packages
are installed
What version of
the package do we need
13
Manually added packages to package.json?
Just use npm install without specifying a package name to install all of them!
14
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!
require( );
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.
15
2.Updating and checking● npm update● npm outdated
16
npm update
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.
17
StableThis is the safest
option as this has no further
changes
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?
18
npm outdated
Use npm outdated to check whether any packages need to be updated.
19
3.Libraries to check out
● jQuery.extend● Underscore● CoffeeScript● Colors
20
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.
21
UnderscoreThe npm package explains this perfectly.
Underscore.js is a utility-belt library for JavaScript that provides support for the usual functional suspects (each, map, reduce, filter...) without extending any core JavaScript objects.
npm install underscore Offers a full library of useful functions.
Do you really need all of them?
22
CoffeeScriptCoffeeScript is a language that compiles into JavaScript, it’s syntax follows that of a mix of Ruby and Python. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.
Example:
npm install coffee-script
More readable and cleaner than JS.
Debugging is a pain.
23
ColorsAdd coloured text to your Node.js console.
npm install colors
YOU GET COLOURS!
Nothing colours rule.
24
4.Publishing your own package
● npm adduser● npm publish● npm unpublish
25
npm adduser
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:
■ Username
■ password
Fill out the fields and continue, you should now be
registered.26
use npm config ls to check whether your credentials are
saved on your system.
27
Simple package - Default file name “index.js”
Requiring it after publishing
28
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.
29
npm adduser npm init npm publish
The process is simple
30
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.
31
5.Extra npm commands● npm view● npm root● npm search● npm help-search● ...
32
List of all npm commands● access● adduser● bin● bugs● build● bundle● cache● completion● config● dedupe● deprecate● dist-tag● docs● edit
● explore● help● help-search● init● install● link● logout● ls● npm● outdated● owner● pack● ping● prefix
● prune● publish● rebuild● repo● restart● rm● root● run-script● search● shrinkwrap● star● stars● start● stop
● tag● test● uninstall● unpublish● update● version● view● whoami
Dont panic you don’t need to use around 70%!
33
Helpful npm commands
npm viewThis command shows data about a package and prints it to the console.
npm searchSearch npm to check whether a package exists.
npm rootDisplays the path in the directory to the node_modules folder.
● npm view <p_name> ● npm search <p_name> ● npm root
34
Helpful npm commands - part 2
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
35
npm explore <p_name>
This command will open a subdirectory in the command line to the package in the node_modules folder. Super useful!
36
6.Programming with npm● npm.commands● npm.load
37
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.
38
Calling commands
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.
39
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.
40
test.js with the prompt package integrated (top), program running in cmd (bottom).41
7.END
● Credits
42
170, 699That’s a lot of packages
100%Guaranteed to speed up your workflow
Around 30k installs a dayAnd a lot of updates
43
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.
44
Credits
Special thanks to all the people who made and released these awesome resources for free:
■ Design by SlidesCarnival
45
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.
46