Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Split Part-02 into two episodes

  • Loading branch information...
commit 7868d5663c2b450df03a9d2d026d03be22c5dc34 1 parent 2272913
Christopher Roach authored

Showing 2 changed files with 87 additions and 67 deletions. Show diff stats Hide diff stats

  1. +73 0 02-01.md
  2. +14 67 02.md → 02-02.md
73 02-01.md
Source Rendered
... ... @@ -0,0 +1,73 @@
  1 +# Node.js: Step by Step - NVM
  2 +
  3 +## Introduction
  4 +
  5 +Hi guys. Well it's been a little while since our last session, and I do apologize for such a long hiatus, but I'm back now, and I'm looking forward to continuing this series on node development.
  6 +
  7 +The next two sessions will be a little different from the first two. We've got a bit of a taste for node already in the previous sessions, and now I want to go off on a bit of a tangent and dive into some of the infrastructure that's grown up around node to help make developing in it so much easier. Over the course of the next two sessions we'll be taking a look at two tools that will help you manage your node development environment and make you more productive.
  8 +
  9 +First on our list is the Node Version Manager, or nvm as it's usually referred to, and that's the tool that we'll be covering in today's session. As its name would suggest, it's a tool for managing multiple versions of Node.
  10 +
  11 +Node is still in it's pre-1.0 phase and, as such, can be a bit volatile. As a developer it pays to have a way of locking a project into a specific version of Node once you've started developing with it, and nvm is going to be our tool of choice for doing just that.
  12 +
  13 +So, apologies made, and summary out of the way, let's get right down to business.
  14 +
  15 +## Node Version Manager (nvm)
  16 +
  17 +In the interim since our last session, node has advanced from release 0.4.5 to 0.6.15. Considering the speed at which node is advancing, it's vital that we have a tool that allows us to create sandboxed development environments that are protected from all the volatility in the node world, and nvm is that tool. Couple it with npm, which we'll be discussing in our next session, and you can create virtual environments around both specific versions of node and any third party libraries that you'll be using in your projects. If you're familiar at all with virtualenv for Python, you'll have some idea of what the combination of these two pieces of software has to offer.
  18 +
  19 +### Installation
  20 +
  21 +The installation of nvm is very simple, just go to its github page in your browser (you can find it by simply googling for nvm and, more than likely, it will be one of the first few results---if not the first). The installation is basically just a matter of storing the nvm directory somewhere in your file system and then sourcing the main shell script whenever you start a new shell. So, if you have git installed on your computer, you can just clone the repository to your local machine and rename it to .nvm in your home directory.
  22 +
  23 + git clone git://github.com/creationix/nvm.git ~/nvm
  24 +
  25 +All this line is going to do is clone the nvm git repository to your home directory under the name nvm. Now I typically like to have these types of folders hidden, so I'm just going to go ahead and change the name of the nvm to .nvm to make make it hidden. This is a personal preference of mine, so feel free to skip this step, but just remember when updating your startup script, which we will be doing as our next step, you'll need to use the original name of the directory (i.e. nvm) rather than the name that I've just given it.
  26 +
  27 +So, now that we've got the source for nvm installed on our system, the next step will be to add a little bit of code to our shell's config file, such as your .bashrc file, to make the environment aware of nvm. Let's go ahead and open that up now. So, here I'm going to pull up my .bashrc file---you'll need to place the following somewhere within this file:
  28 +
  29 + export NVM_HOME="$HOME/.nvm"
  30 + if [[ -f "$NVM_HOME/nvm.sh" ]]; then
  31 + source "$NVM_HOME/nvm.sh"
  32 + fi
  33 +
  34 +All that the little bit of code above does is first check for the existence of the `nvm.sh` file within the `.nvm` directory that you just created. Then, if it exists, it runs the file within the current shell creating several new shell functions for you to use. Now, you don't actually need to surround the `source` command with the check for the file's existence since we know it's there, but I like to have it there to keep the shell from screaming at me whenever I remove or move my .nvm directory for whatever reason.
  35 +
  36 +Ok, now that we've got nvm installed, we just need to source our startup file to get nvm to be recognized in the current shell. To make sure everything worked properly, call `nvm` now and hopefully what you'll see is the help output for the command.
  37 +
  38 +If you just saw some help information fly across your screen, you're all setup and ready to manage multiple versions of node.
  39 +
  40 +### Exploring nvm
  41 +
  42 +Now that you have nvm installed, let's play around with it a bit and see what all it has to offer and then get the latest version of node installed on our system.
  43 +
  44 +There are a few important commands that you'll probably use quite often. The first of these is the `ls` command. Go ahead and type `nvm ls` at the command line and take a look at what gets printed out. Not much huh? Just an N/A (or a dot depending on your shell) and a second line that says `current:`. The reason your not seeing much here is that you haven't installed node on your system yet. Once you've got at least one node installed, the `ls` command becomes much more useful by listing all of the currently installed versions of node available for you to choose from as well as the current version in use and any aliases that you've created. Well I can't think of a better segue than that to start installing a copy of node. So, let's do that now.
  45 +
  46 +To install a version of node you invoke the `nvm install` command with the version number of the Node you want to install. For us that will be version 0.6.15, but an important thing to remember here is that the version number must be prefaced with a 'v' or else the install will not work. So, type the command `nvm install v0.6.15` at your command line now and you should see the download and installation begin. The installation is going to take a little while, so I'm just going to skip ahead to the end, so you may want to pause this screencast now until your installation is complete.
  47 +
  48 +Now that we have a version of node installed on our system, let's take another look at the output of `nvm ls`. Notice that this time it's listing our newly installed version of Node. It also lists this version as the current version that we are using. You can confirm this by typing `node -v` and making sure that a version number, rather than an error message, pops up.
  49 +
  50 +What would happen though, if we opened another terminal session? Would our system still default to the most recently installed version of node or something else? Let's try it out and see what happens. If we open a new terminal window and type `node -v` again, this time we're greeted with an error message telling us that the 'node' command doesn't exist. If we type `nvm ls` we see why this is, we haven't selected a node to use as our current node. To do so we use the `use` command. So, let's go ahead and select version 0.6.15 as our current node. Type `nvm use 0.6.15`. With the `use` command the 'v' prefix is optional, though I'll show a reason why you might want to use it in just a second. Now if we type `nvm ls` again, we should see that our current node version is 0.6.15 and typing `node -v` this time should confirm it.
  51 +
  52 +So, `nvm use` is the command we use to switch between all the different versions of node we have installed on our system. But how do we select one version to always be our default current version? Well you do that through the alias command.
  53 +
  54 +nvm's alias command allows us to setup several different names for versions we use often, as a way of more easily identifying them. If we run `nvm help` we see here that we can create a new alias by typing `nvm alias` followed by the alias name and by the version number we want to associate with that name. So, for example, if we executed the command `nvm alias latest 0.6.15` then from that moment on, we could refer to version 0.6.15 as simply 'latest'. So the next time we wanted to use that version, we could just type `nvm use latest` and voila. Much easier than typing in the version number, and it allows us to label specific versions according to which projects we were using them for.
  55 +
  56 +One alias in particular though that is treated specially by nvm is the 'default' alias. nvm will automatically load the version of node aliased to 'default' as the current version whenever a new shell session is created. So let's go ahead and set our default version now by typing `nvm alias default v0.6.15`. Now if we start a new shell session and type `node -v` we see that the latest version has already been selected for us and typing `nvm ls` will confirm that. Notice also that underneath the Current version of node is a list of all of our aliases that we've defined. Since we've aliased the latest version of node to default, we probably don't actually need another alias for it, so let's go ahead and get rid of the 'latest' alias we created earlier by typing `nvm unalias latest` and then type `nvm ls` just to confirm that it's been removed.
  57 +
  58 +There's one more thing I want cover before we finish up today, and that's an option that was recently added to nvm that supports tab completion in Bash. Adding this feature is extremely simple, it's just a matter of sourcing another file upon starting a new shell session like we did before for the installation of nvm.
  59 +
  60 +So, let's open up our .bashrc file again and go back to where we put the few lines of code for sourcing nvm. Right below that we're going to place another few lines that will essentially do the same thing as before. As you can see here, we will first make sure that the bash_completion file exists, and then, if it does, we'll source that file.
  61 +
  62 +Let's give it a try. Go ahead and save your changes and exit back out to the command line. Then source the .bashrc file to make sure that it's added the tab completion to our current session. Then type `nvm h` and hit the tab button and it should complete the word help. Let's try that again with a version number. Type `nvm use v` then tab and it should complete the version 0.6.15 (assuming that you haven't installed anymore versions). And, that's tab completion. Not an absolute must, but still a feature that makes using nvm much nicer all around.
  63 +
  64 +### Conclusion
  65 +
  66 +Well, that pretty much wraps everything up for today's episode. That should be just enough to get you started with nvm. The app is fairly simple to understand and you should be able to pick up the rest by just looking at the help output from `nvm help`.
  67 +
  68 +As I mentioned quickly at the begining of this session, we'll be covering npm in our next episode. npm is the package manager for Node and compliments nvm quite nicely. The combination of the two make it extremely easy to create completely sandboxed development environments that you can switch between with just a few commands. We'll also install our very first Node package and put it to use helping us out with our development. So, please come back and join me again for part 2 of this look at some of the tools that you'll find indespensible in your Node development.
  69 +
  70 +I'll see you then.
  71 +
  72 +
  73 +[nvm]:https://github.com/creationix/nvm
81 02.md → 02-02.md
Source Rendered
... ... @@ -1,75 +1,18 @@
1   -# Node.js: Step by Step - NVM and NPM
  1 +# Node.js: Step by Step - NPM
2 2
3 3 ## Introduction
4 4
5   -Hi guys. Well it's been a little while since our last session, and I do apologize for such a long hiatus, but I'm back now and I'm looking forward to continuing this series on node development.
  5 +In our last episode we covered the NVM tool for managing the different versions of node that we need installed on our system for development. Today we're going to cover npm, a tool used managing software packages in Node.
6 6
7   -Today's session will be a little different from the first two. We've got a bit of a taste for node already in the previous sessions, and now I want to go off on a bit of a tangent and dive into some of the infrastructure that's grown up around node to help make developing in it much easier. Today will be all about learning a couple of the most important tools for developing Node applications that will, hopefully, make you much more effective and will also aid you in the completion of this series.
8   -
9   -The tools that we'll be covering today are [nvm][nvm] and [npm][npm].
10   -
11   -First on our list is the Node Version Manager, or nvm as it is usually referred to. As its name would suggest, it's a tool for managing multiple versions of Node. Node is still in it's pre-1.0 phase and, as such, can be a bit volatile. As a developer it pays to have a way of locking a project into a specific version of Node once you've started developing with it and nvm is going to be our tool for doing just that.
12   -
13   -Next on our list is npm, which while its website states that npm officially doesn't stand for anything, it is in fact the package manager for node, so I'll let you draw your own conclusions on what the name really means. Like gems for Ruby or pip for Python, it allows us to install and manage any number of third party libraries without all the worries of finding and installing dependencies and so on and so forth.
  7 +So what is npm? Well, while its website states that npm officially doesn't stand for anything, it is in fact the package manager for node, so I'll let you draw your own conclusions on what the name really means. Like gems for Ruby, or pip for Python, it allows us to install and manage any number of third party libraries without all the worries of finding and installing dependencies and so on and so forth.
14 8
15 9 Finally, as a bonus, we'll put npm to use by installing another third party program from the maker of npm that should help make developing Node apps much less painful.
16 10
17   -So, apologies made and summary out of the way, let's get right down to business.
18   -
19   -## Node Version Manager (nvm)
20   -
21   -We'll start off by taking a look at nvm. In the interim since our last session, node has advanced from release 0.4.5 to 0.6.15. Considering the speed at which node is advancing, it's vital that we have a tool that allows us to create sandboxed development environments that are protected from all the volatility in the node world, and nvm is that tool. Couple it with npm, which we'll be discussing next, and you can create virtual environments around both specific versions of node and any third party libraries that you'll be using in your projects. If you're familiar at all with virtualenv for Python, you'll have some idea of what the combination of these two pieces of software has to offer.
22   -
23   -### Installation
24   -
25   -The installation of nvm is very simple, just go to its github page in your browser (you can find it by simply googling for nvm and, more than likely, it will be one of the first few results---if not the first). The installation is basically just a matter of storing the nvm directory somewhere in your file system and then sourcing the main shell script whenever you start a new shell. So, if you have git installed on your computer, you can just clone the repository to your local machine and rename it to .nvm in your home directory.
26   -
27   - git clone git://github.com/creationix/nvm.git ~/nvm
28   -
29   -All this line is going to do is clone the nvm git repository to your home directory under the name nvm. Now I typically like to have these types of folders hidden, so I'm just going to go ahead and change the name of the nvm to .nvm to make make it hidden. This is a personal preference of mine, so feel free to skip this step, but just remember when updating your startup script, which we will be doing as our next step, you'll need to use the original name of the directory (i.e. nvm) rather than the name that I've just given it.
30   -
31   -So, now that we've got the source for nvm installed on our system, the next step will be to add a little bit of code to our shell's config file, such as your .bashrc file, to make the environment aware of nvm. Let's go ahead and open that up now. So, here I'm going to pull up my .bashrc file---you'll need to place the following somewhere within this file:
32   -
33   - export NVM_HOME="$HOME/.nvm"
34   - if [[ -f "$NVM_HOME/nvm.sh" ]]; then
35   - source "$NVM_HOME/nvm.sh"
36   - fi
37   -
38   -All that the little bit of code above does is first check for the existence of the `nvm.sh` file within the `.nvm` directory that you just created. Then, if it exists, it runs the file within the current shell creating several new shell functions for you to use. Now, you don't actually need to surround the `source` command with the check for the file's existence since we know it's there, but I like to have it there to keep the shell from screaming at me whenever I remove or move my .nvm directory for whatever reason.
39   -
40   -Ok, now that we've got nvm installed, we just need to source our startup file to get nvm to be recognized in the current shell. To make sure everything worked properly, call `nvm` now and hopefully what you'll see is the help output for the command.
41   -
42   -If you just saw some help information fly across your screen, you're all setup and ready to manage multiple versions of node.
43   -
44   -### Exploring nvm
45   -
46   -Now that you have nvm installed, let's play around with it a bit and see what all it has to offer and then get the latest version of node installed on our system.
47   -
48   -There are a few important commands that you'll probably use quite often. The first of these is the `ls` command. Go ahead and type `nvm ls` at the command line and take a look at what gets printed out. Not much huh? Just an N/A (or a dot depending on your shell) and a second line that says `current:`. The reason your not seeing much here is that you haven't installed node on your system yet. Once you've got at least one node installed, the `ls` command becomes much more useful by listing all of the currently installed versions of node available for you to choose from as well as the current version in use and any aliases that you've created. Well I can't think of a better segue than that to start installing a copy of node. So, let's do that now.
49   -
50   -To install a version of node you invoke the `nvm install` command with the version number of the Node you want to install. For us that will be version 0.6.15, but an important thing to remember here is that the version number must be prefaced with a 'v' or else the install will not work. So, type the command `nvm install v0.6.15` at your command line now and you should see the download and installation begin. The installation is going to take a little while, so I'm just going to skip ahead to the end, so you may want to pause this screencast now until your installation is complete.
51   -
52   -Now that we have a version of node installed on our system, let's take another look at the output of `nvm ls`. Notice that this time it's listing our newly installed version of Node. It also lists this version as the current version that we are using. You can confirm this by typing `node -v` and making sure that a version number, rather than an error message, pops up.
53   -
54   -What would happen though, if we opened another terminal session? Would our system still default to the most recently installed version of node or something else? Let's try it out and see what happens. If we open a new terminal window and type `node -v` again, this time we're greeted with an error message telling us that the 'node' command doesn't exist. If we type `nvm ls` we see why this is, we haven't selected a node to use as our current node. To do so we use the `use` command. So, let's go ahead and select version 0.6.15 as our current node. Type `nvm use 0.6.15`. With the `use` command the 'v' prefix is optional, though I'll show a reason why you might want to use it in just a second. Now if we type `nvm ls` again, we should see that our current node version is 0.6.15 and typing `node -v` this time should confirm it.
55   -
56   -So, `nvm use` is the command we use to switch between all the different versions of node we have installed on our system. But how do we select one version to always be our default current version? Well you do that through the alias command.
57   -
58   -nvm's alias command allows us to setup several different names for versions we use often, as a way of more easily identifying them. If we run `nvm help` we see here that we can create a new alias by typing `nvm alias` followed by the alias name and by the version number we want to associate with that name. So, for example, if we executed the command `nvm alias latest 0.6.15` then from that moment on, we could refer to version 0.6.15 as simply 'latest'. So the next time we wanted to use that version, we could just type `nvm use latest` and voila. Much easier than typing in the version number, and it allows us to label specific versions according to which projects we were using them for.
59   -
60   -One alias in particular though that is treated specially by nvm is the 'default' alias. nvm will automatically load the version of node aliased to 'default' as the current version whenever a new shell session is created. So let's go ahead and set our default version now by typing `nvm alias default v0.6.15`. Now if we start a new shell session and type `node -v` we see that the latest version has already been selected for us and typing `nvm ls` will confirm that. Notice also that underneath the Current version of node is a list of all of our aliases that we've defined. Since we've aliased the latest version of node to default, we probably don't actually need another alias for it, so let's go ahead and get rid of the 'latest' alias we created earlier by typing `nvm unalias latest` and then type `nvm ls` just to confirm that it's been removed.
61   -
62   -There's one more thing I want cover before moving onto npm, and that's an option that was recently added to nvm that supports tab completion in Bash. Adding this feature is extremely simple, it's just a matter of sourcing another file upon starting a new shell session like we did before for nvm.
63   -
64   -So, let's open up our .bashrc file again and go back to where we put the few lines of code for sourcing nvm. Right below that we're going to place another few lines that will essentially do the same thing as before. As you can see here, we will first make sure that the bash_completion file exists, and then, if it does, we'll source that file.
65   -
66   -Let's give it a try. Go ahead and save your changes and exit back out to the command line. Then source the .bashrc file to make sure that it's added the tab completion to our current session. Then type `nvm h` and hit the tab button and it should complete the word help. Let's try that again with a version number. Type `nvm use v` then tab and it should complete the version 0.6.15 (assuming that you haven't installed anymore versions). And, that's tab completion. Not an absolute must, but still a feature that makes using nvm much nicer all around.
67   -
68   -Well, that should be enough to get you started with nvm. The app is fairly simple to understand and you should be able to pick up the rest by just looking at the help output from `nvm help`. Now I want to move onto the more interesting stuff and play around with npm a bit. So let's get that installed and see what all it can do for us.
  11 +So, let's get started...
69 12
70 13 ## Node's Package Manager (NPM)
71 14
72   -NPM is one tool that I think makes node worth using even if you're not completely sold on all of node's other strengths. I consider it to be the new standard that all package management systems should strive to live up to. I hope by the end of this screencast you'll agree with me, and that you'll have some idea as to just how powerful and easy to use NPM is, and, in a future screencast, I hope to cover it a bit further by showing off just how easy it is to create and distribute your own packages. But that's enough praise for the program, let's go ahead and get it installed and start playing around with it a bit.
  15 +NPM is one tool that I think makes node worth using even if you're not completely sold on all of node's other strengths. I consider it to be the new standard by which all package management systems should be judged. I hope by the end of this screencast you'll agree with me, and that you'll have some idea as to just how powerful and easy to use NPM is, and, in a future screencast, I hope to cover it a bit further by showing off just how easy it is to create and distribute your own packages. But that's enough praise for the program, let's go ahead and get it installed and start playing around with it a bit.
73 16
74 17 ### Installation
75 18
@@ -101,7 +44,11 @@ Now let's change the "viewer" variable to "browser" by typing `npm config set vi
101 44
102 45 If "browser" was returned, you've successfully updated the "viewer" configuration setting and you're ready to start viewing the npm documentation in glorious technicolor, er, um, HTML. Go ahead and give it a try now by typing `npm help npm` into the command line. Hopefully, your default browser opened up with the documentation for npm in it. If not, you may need to install Chrome for this feature to work.
103 46
104   -To revert the configuration, we can simply delete it. We can see why this is true by taking a look at our current npm configuration settings with the `list` command (or you can use `ls` for short). There are two things to notice from the output of the `list` command. First, you can see that all the `config` command does is manage a user-specific configuration file called `.npmrc` found in your home directory. So you could simply bypass the command and set everything there by hand. The second thing to notice is that the `list` command is only showing us the settings that we've updated. If we want to view all of the settings---including the default settings---we'll need to add the long, or `-l`, option to the command. Go ahead and try that now by typing `npm config ls -l` and you should see a whole slew of settings fly by.
  47 +To revert the configuration, we can simply delete it. We can see why this is true by taking a look at our current npm configuration settings with the `list` command (or you can use `ls` for short). There are two things to notice from the output of the `list` command.
  48 +
  49 +First, you can see that all the `config` command does is manage a user-specific configuration file called `.npmrc` found in your home directory. So you could simply bypass the command and set everything there by hand.
  50 +
  51 +The next thing to notice is that the `list` command is only showing us the settings that we've updated. If we want to view all of the settings---including the default settings---we'll need to add the long, or `-l`, option to the command. Go ahead and try that now by typing `npm config ls -l` and you should see a whole slew of settings fly by.
105 52
106 53 Given that all we're doing by changing the "viewer" setting is essentially overriding the default value, it would stand to reason that we can simply delete the setting from our config file and it should revert back to the default. Let's do that now by typing `npm config delete viewer` and press return. A quick check with `npm get viewer` confirms that the configuration setting has been reverted and running `npm help npm` confirms it further by opening up the documentation in the terminal rather than the browser.
107 54
@@ -215,9 +162,11 @@ Now that we've got it installed, let's give it a try. In your app's directory, c
215 162
216 163 Alright, well that pretty much wraps up everything. I know this has been a long episode, but there was just so much to cover.
217 164
218   -You've learned about what I consider to be the most important tools in the node developer's arsenal. NVM is a great way to manage multiple node versions on one machine, and if you don't know and understand npm, you're essentially going to be useless as a node developer. Finally, we installed and took a look at supervisor, a really handy little program that helps shrink our development time by making it possible to see our changes take effect immediately in the browser.
  165 +Over the course of the past two episodes you've learned about, what I consider to be, the most important tools in the node developer's arsenal. NVM is a great way to manage multiple node versions on one machine, and if you don't know and understand npm, you're going to find that you are continuously reinvinting the wheel when developing in Node. Finally, we installed, and took a look at supervisor, a really handy little program that helps shrink our development time by making it possible to see our changes take effect immediately in the browser.
219 166
220   -I hope that you've all gotten something out of this episode and I look forward to talking to you all again very soon.
  167 +I hope that you've all gotten something out of this and the last episode and I look forward to getting in back into the development of our app in our next session. So, please, join me again for the next episode of Node: Step by Step.
  168 +
  169 +I'll talk to you all again very soon.
221 170
222 171
223 172
@@ -288,7 +237,5 @@ I hope that you've all gotten something out of this episode and I look forward t
288 237 alias cd="env_cd"
289 238
290 239
291   -[nvm]:https://github.com/creationix/nvm
292 240 [npm]:http://npmjs.org/
293 241 [npm-g-vs-l]:http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/
294   -

0 comments on commit 7868d56

Please sign in to comment.
Something went wrong with that request. Please try again.