Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Grunt

You're reading from   Learning Grunt Monitor and automate complex JavaScript tasks and processes by obtaining a practical understanding of Grunt

Arrow left icon
Product type Paperback
Published in Mar 2016
Publisher Packt
ISBN-13 9781785888809
Length 184 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Douglas Reynolds Douglas Reynolds
Author Profile Icon Douglas Reynolds
Douglas Reynolds
Arrow right icon
View More author details
Toc

Upgrading or installing Node.js

In this section, we will go through the process of the Node.js installation. As of the time of writing this, the current version of Node.js is v4.3.1. Some readers may already be up and running with Node; for others, this may be the first introduction to the server platform. As a result, we will cover the steps necessary for both an upgrade and a new installation:

  • Upgrading Node.js via NPM on Mac
  • Upgrading Node.js via .msi and NPM
  • The pristine installation of Node.js via a downloaded binary
  • Adding your installation path to your $PATH variable

If you do not have Node.js installed on your machine, feel free to jump to the "Pristine Installation of Node.js via downloaded binary" section. If you have a previous version of Node installed, know that Grunt requires Node versions greater than or equal to 0.8.0. If you want to upgrade, it is strongly recommended that you perform a backup prior to performing the Node.js upgrade process. For those on Mac, continue reading the next section. For those of you on Windows, feel free to jump to the Upgrading Node.js via .msi and NPM section.

Upgrading Node.js via NPM on Mac

Upgrading Node.js via NPM is very straightforward and shows you just how great a utility NPM is. We will use a Terminal application in order to update Node from the command-line interface. This process assumes that you have an existing instance of Node.js installed globally. If you do not have Node.js installed as a global application, you will need to change directories to the location where Node.js is installed and perform the update from this location. A global installation of Node.js provides an entry to your PATH variable and you will be able to access it via the command line from any directory. A local installation might be in a project root where you are using specific Node.js versions in a project.

It is important to note that the upgrade process uses a Node binary manager module called n. The n module is not maintained by Node.js. Therefore, proceed with caution and at your own risk when performing the upgrade process that follows. Information on the n module can be found at its GitHub page, https://github.com/tj/n. It is strongly suggested that you review the project's documentation prior to proceeding with an upgrade. Additionally, make sure that you have a recent backup.

With this said, I have used the following upgrade process many times without the slightest issue. I am extremely confident in using the n module to upgrade my own Node.js versions.

To get started, first check for the current version of Node that is installed on your machine:

$ node --version

You can also use another command showed as follows:

$ node –v

This will display the currently installed version of node. Your result may differ but should look something like the following version, which was the output from calling the version command:

v0.10.29

The next line deletes the contents of the NPM cache so that we can ensure that we are not working with stale files/data. The sudo requests administrator privilege, npm defines the application to be used, cache manipulates the packages cache, and clean deletes the data from the cache directory. Depending on your user, you may need to include a super user prefix, such as sudo, in order to have permissions to perform the following commands. If you get an error message that alludes to permissions, then your user does not have sufficient privilege and the use of super user prefix or logging in as administrator user will be needed. Syntax using sudo is provided as it doesn't hurt anything to include it in the commands; however, it may not be necessary, depending on whether your installation exists globally or locally:

$ sudo npm cache clean -f

If you are using sudo and are not currently logged in as an administrator, you will be prompted for your password in the next line. If you are not using sudo, then this step will be omitted:

$ password:

Enter your password and press ENTER to continue.

You may now be presented with an NPM warning that notifies you that NPM is using force. This is seen because of the -f flag used in the command that was issued previously:

$ npm WARN using --force I sure hope you know what you are doing.

Continue with the installation. In the following line, I am using the sudo administrator to run the npm application in order to install the n application globally by using -g. I will use the n module to upgrade Node.js:

$ sudo npm install -g n

As n is installed, I will see some path information displayed, which shows where n has been installed. When this is complete, I am ready to upgrade node with the following command. The sudo administrator will use the n application to install the latest stable version of Node.js:

$ sudo n stable

The command runs n with the stable option. This will get the latest stable version of Node.js from the server. You can also request a specific version by replacing stable with the version string that you wish to install, for example, sudo n 0.12.7.

At this point, you should see a result similar to the following, which confirms the version being installed along with the path information. In this case, the result is that node-v0.12.7 was installed in a new directory located in /usr/local/n/versions/node/0.12.7. Confirmation of the installed version shows v0.12.7. Your version may differ significantly, so don't be alarmed by the version number represented here:

install : node-v0.12.7
       mkdir : /usr/local/n/versions/node/0.12.7
       fetch : https://nodejs.org/dist/v0.12.7/node-v0.12.7-darwin-x64.tar.gz
   installed : v0.12.7

The entire Terminal series of commands should resemble the following screenshot. If, by chance, your node version was already up to date, n will not provide any output, as shown in the following screenshot:

Upgrading Node.js via NPM on Mac

At this point, you have either successfully upgraded Node.js or discovered that your version of Node.js is up to date. In either case, you are now prepared to move forward to add your installation path to your $PATH variable.

Congratulations! You have updated Node.js.

Be sure to have a look at the n GitHub pages for other useful commands, such as 'rm' to remove Node.js. If you have multiple versions of Node.js installed, you can specify, similar to the installation, the version that you want to remove using the rm command.

Upgrading Node.js on Windows via .msi and NPM

For those of you that currently have Node installed on Windows, the process of upgrading is basically the same as that of a new installation. The simplest approach is to use the Node Windows Installer, .msi, available from the Node.js downloads page, https://nodejs.org/download/:

Upgrading Node.js on Windows via .msi and NPM

Prior to beginning, check your current version of Node so that you can verify that you have successfully updated Node and NPM when the installation process was completed:

$ node –version

You can also use another command shown as follows:

$ node –v

Your expected output should be the version of Node that is currently installed on your machine:

v0.10.29

Choose the Windows Installer .msi file that is appropriate for your machine and download it. Once the download is completed, run the installer file to begin the Node.js installation. Follow the prompts to run the installation, accept the terms of the license agreement, and continue completing the installation.

The final step of the Windows upgrade process is to update all of the existing global packages. This will require cleaning of the package cache and then performing an update of all global packages. We can do this in two lines with the following commands:

npm cache clean
npm update -g

You have successfully upgraded Node.js or discovered that your version of Node.js is up to date. At this point, you are now prepared to move forward to add your installation path to your $PATH variable.

Congratulations, you have successfully updated Node.js!

The pristine installation of Node.js via downloaded binary

For those of you who do not have a version of Node.js installed yet, the following steps for the installation are for you. The examples illustrated here are from the Mac installer; while the Windows installer will be a little different, the installation process is basically the same. For this installation, we will download the binary installation file and install it on your machine. The current version as of the writing of this book is 4.3.1. Navigate to https://nodejs.org/download/ to find the available downloads for various platforms:

The pristine installation of Node.js via downloaded binary

Choose the correct download link for your platform. For the purposes of this book, we will use a binary installer (.exe for Windows or .pkg for Mac) and will not be building Node.js from source. The examples that follow will be for the Mac platform. If you are installing in Windows, simply follow the steps outlined in the installer application.

After downloading the binary, run the installer file to open the installation dialogue. The first view that we are presented with is the Introduction. Note the line defining the installation path. Click on Continue to proceed to review and accept the license agreement:

The pristine installation of Node.js via downloaded binary

Click on Continue, and once you have reviewed the agreement, then Agree to the license that is presented in the pop-up window that is launched:

The pristine installation of Node.js via downloaded binary

Select the Install for all users of this computer option to enable the Continue button in order to proceed:

The pristine installation of Node.js via downloaded binary

Press Install in order to accept the default location shown in step 1 of the installation:

The pristine installation of Node.js via downloaded binary

Node.js will be installed in the default, or specified, directory. You will then be presented with the summary view of the installation:

The pristine installation of Node.js via downloaded binary

Congratulations, you have successfully updated Node.js!

Verify that your install location for node is on your path by checking for node's version from the Terminal command line:

$ node –version

If you receive an error resembling the following, then the installation path needs to be added to your $PATH variable:

-bash: node: command not found

The next section will cover how to add the installation path to your $PATH variable.

Adding your installation path to your $PATH variable

For Windows 7, the process of editing the Path variable is as follows:

  1. Choose Properties from the My Computer context menu (right-click).
  2. Choose the Advanced tab from the System Properties window.
  3. Click on the Environment Variables button in the Advanced section of the window.
  4. Select the Path item from the list of variables and click on the Edit button.
  5. Append the path where node was installed to the Path variable; they are separated by semicolons.
  6. Choose OK to save the changes to Path:
    Adding your installation path to your $PATH variable

For Mac OS X, we will edit the $PATH variable from within Terminal using the Nano editor. You can use vi editor as well for the same purpose. It is even possible to use Text Edit.

To begin, let's first check our existing $PATH variable value by echoing the path variable in Terminal:

$ echo $PATH

You should see a result with the current value of your $PATH variable, the following for example:

$ /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

Next, change directories to the home directory using the Change Directory (cd) command:

$ cd

Create a .bash_profile file if it does not exist, or open the .bash_profile file in nano for the editing. This file exists at the root of your user directory:

$ nano .bash_profile

If you prefer to use Text Edit, you can use the open command with [-e], which instructs the command to use Text Edit:

open -e .bash_profile

You can use [-a] in the command to define the specific application to open the file by defining a named application:

open -a TextEdit .bash_profile

You should see the contents of your .bash_profile file in the nano editor. It will be empty if you have never created one before. In the following example, I have already defined locations to be included in my path. As you can see, my .bash_profile already exists and has content in it:

Adding your installation path to your $PATH variable

Add the following line to the file, where [PATH_TO_NODE_INSTALL_DIRECTORY] is the path where the Node installer application installed Node.js:

PATH=$PATH:[PATH_TO_NODE_INSTALL_DIRECTORY]

Save the file in nano by pressing Ctrl + O and confirm that the filename is to be saved as .bash_profile by hitting return. Exit nano by pressing Ctrl + X.

You can exit terminal and reopen in order to start a new terminal session and allow the changes to take effect; however, an even better way is to enter the following command to reload your .bash_profile:

$ source .bash_profile

In this method, there is no need to restart the Terminal application; the source command executes the file defined as the argument—in this case, .bash_profile—thus the $PATH variable has been updated.

Using NPM to install the Grunt Command Line Interface

Up until this point, we have been working on installing the dependencies of Grunt. Now, we finally get to start working on the installation of the Grunt CLI. Installing the Grunt CLI is the first step in the process of setting up automated tasks with Grunt and we will use the CLI for all of our grunt work. While this was a pun and not that funny, the CLI has one specific purpose: to be able to run the grunt command from anywhere on your system. This is why we will be installing the CLI globally, which means that it will be able to run from any location, globally, on your machine.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime