Lesson 1. Configuring your environment

In this lesson, you install all the tools you need to start building applications with Node.js. You install a version of Node.js that’s compatible with the latest JavaScript ES6 updates. Next, you install a text editor—software through which you’ll write your application’s code. Last, you give Node.js a test drive from your computer’s command-line terminal by using a Node.js sandbox environment known as REPL.

This lesson covers

  • Installing Node.js
  • Installing a text editor
  • Setting up SCM and deployment tools
  • Working with the Node.js REPL in terminal

1.1. Installing Node.js

Node.js is growing in popularity and support. For this reason, new versions to download are being deployed quite frequently, and it’s important to stay up to date with the latest versions to see how they may benefit or otherwise affect the applications you’re building. At this writing, the version of Node.js to download is 11.0.0 or later.

Note

The release of Node.js 8.8.1 comes with support for ES6 syntax. ES6 (ECMAScript 2015) is a recent update to JavaScript, with syntax improvements for defining variables, functions, and OOP code. To keep up with updates to JavaScript, download the latest stable version of Node.js as your development progresses.

You have a couple of ways to download and install Node.js, all of which are listed on the Node.js main site, https://nodejs.org.

Because Node.js is platform-independent, you can download and install it on your Mac, Windows, or Linux computer and expect full functionality.

The simplest way to install Node.js is to go to the download link at https://nodejs.org/en/download/ and follow the instructions and prompts to download the installer for the latest version of Node.js (figure 1.1).

Figure 1.1. Node.js installer page

Node Version Manager

Alternatively, you may want to use the Node.js Version Manager (NVM) to handle your Node.js installation and manage one version or multiple versions of Node.js on your computer. The benefit of using a version manager is that you can test newer versions of Node.js as they’re released while still having older, more stable, versions installed in case of compatibility issues. You can follow the installation instructions at https://github.com/creationix/nvm or follow these steps on a UNIX machine:

  1. Run curl -o https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash in a new terminal window. You may need to quit and relaunch terminal after this installation completes.
  2. Run nvm list in a terminal window to see whether any versions of Node.js are already installed on your computer.
  3. Run nvm ls-remote in terminal to check what versions of Node.js are available to install.
  4. Run nvm install 11.0.0 in terminal to install the current Node.js version.
  5. Run node -v in terminal to verify that you have version 9.3.0 installed.

If you’re comfortable with installing Node.js through NVM and without a graphical interface to walk you through the process, this setup is right for you. When installation is complete, don’t install Node.js again by using the other set of instructions in this lesson.

Note

NVM doesn’t support Windows. You may work with one of two alternative version managers: nvm-windows and nodist, which you can install by following the instructions at https://github.com/coreybutler/nvm-windowsandhttps://github.com/marcelklehr/nodist, respectively.

When you install Node.js, you also get npm, the Node.js ecosystem of external libraries (multiple files of code other people wrote) that can be imported into your future projects. npm is similar to pip in Python and gem in Ruby. You learn more about npm in unit 1.

When the installer file is downloaded, double-click the file in your browser’s download panel or your computer’s download folder. The installer opens a new window that looks like figure 1.2 and writes all necessary files and core Node.js libraries to your system. You may be asked to accept licensing agreements or give the installer permission to install Node.js on your computer. Follow the prompts to click through the installation.

Figure 1.2. Node.js writing to your machine

Terminal and your PATH

You’ll be working mostly in your computer’s terminal, which is built-in software used to navigate and run commands on your computer without a graphical interface. This book teaches using UNIX terminal (Bash) commands. Those of you who are Windows users can follow along by using Window’s CMD terminal window (but may need to look up command equivalents throughout the book). You can reference the table at https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Step_by_Step_Guide/ap-doslinux.html, which compares Windows and UNIX commands. To make things easier in Windows, you can download and install an additional Bash terminal called Git Bash from http://git-scm.com/downloads.

Make a note of where your version of Node.js and npm are installed on your machine. That information appears in the final window of the installer. The installer attempts to add these directory locations to your system’s PATH variable.

PATH is an environmental variable—a variable that can be set to influence the behavior of operations on your machine. Your computer’s PATH variable specifies where to find directories and executable files needed to perform operations on your system.

This variable’s value is the first place terminal will look for resources used in development. Think of the PATH variable as being like your computer’s index for quickly finding the tools you need. When you add these tools’ original file paths or directory locations to the PATH variable, terminal won’t have any problems finding them.

The following figure shows how terminal refers to the PATH variable to identify directories of certain programs and executable files, as these directories may be in different locations on different computers. If you experience any problems starting Node.js in your terminal, follow the installation steps at https://www.tutorialspoint.com/nodejs/nodejs_environment_setup.htm.

Terminal functionality with PATH variable

Now that you have Node.js installed, use terminal to make sure that everything is installed correctly. Open terminal (or Git Bash), and type the following command at the prompt: node -v.

The output of this command should show you the version of Node.js you installed. Similarly, you can check the version of npm that you installed by running the command npm -v at the command prompt.

Note

If your terminal responds with an error or with nothing, it’s possible that your installation of Node.js was unsuccessful. In the case of an error, try copying and pasting that error into a search engine to look for common solutions. Otherwise, repeat the steps in this section.

Now that you have Node.js installed and your terminal running, you need somewhere to write your code.

Tip

If you ever forget where you installed Node.js or npm, you can open a command window and type which node or which npm at the prompt to see the corresponding location. From a Windows command-line prompt, use where in place of which.

1.2. Installing a text editor

A text editor is a software application you use to write your code while developing an application. Although text editors come in many forms and can be used to make noncode files as well, the text editors designed for developers often come prepackaged with helpful tools and plugins.

For this book, I recommend downloading and installing the Atom text editor, a free open-source software application for developing in many programming languages. Atom was developed by GitHub and offers many additional plugins written in Node.js. Atom will help you write a Node.js application with ease.

Install Atom by following these steps:

  1. In your browser, go to https://atom.io.
  2. Click the Download link.
  3. Follow the prompts to install the software on a Mac, Windows, or Linux computer.

When the installation completes, open the folder on your computer where applications are located. From there, you can launch the Atom editor by double-clicking the program file.

Tip

You may be interested in writing your code in an integrated development environment (IDE). IDEs such as Visual Studio Code (https://code.visualstudio.com/) offer helpful tools like a terminal window within the editor, code autocomplete, and debuggers for your project.

With your text editor in place, test some Node.js terminal commands.

1.3. Setting up SCM and deployment tools

In this section, you set up Git and the Heroku command-line interface (CLI), which you’ll use at the end of the book to deploy your applications online. Deployment is a term used to describe the migration of your application from your computer to a place where it can be accessed and used publicly online. Software configuration management (SCM) is the process of managing your application in its different environments as new features and changes are applied to the code. You can use Git and the Heroku CLI together to deploy your code from development to production and manage your application.

Git is a version-control tool used to separate layers of your application’s code evolution. It allows you to save, or take a snapshot, of your code at different stages of development, making it easy to return to a working state quickly if you find that your latest changes break your application’s functionality. More important for this book, you need Git to send a version of your code to Heroku so that people can start using your application on the internet.

If you have a Mac, Git should already be installed. If you’ve installed Git Bash on your Windows machine, Git came packaged and installed too. If you aren’t sure whether you have Git, you can enter git --version in a terminal window. Unless your window responds with a Git version number, you should download it directly from https://git-scm.com/downloads. Select your operating system, as shown in figure 1.3. The downloaded file opens a graphical interface through which you can install Git on your machine.

Figure 1.3. Installing Git from the downloads page

When Git is installed, you use it by initializing your project with git init in terminal. Then you can add individual project files to your new version by running git add followed by the relative path to the file. You can also add all the files in your project by running git add. (including the period in the command). To confirm these files, run git commit -msome message”, where the message in quotations describes the changes you made. If you’re familiar with Git, I recommend using it as you run the code in this book. Otherwise, you won’t need it until unit 8. You learn more about using Git through videos and documentation at https://git-scm.com/doc.

Tip

For a useful cheat sheet of Git commands, visit https://services.github.com/ondemand/downloads/github-git-cheat-sheet.pdf.

Heroku is a service you’ll use to host your application online. To use Heroku, you need to create a new account at https://signup.heroku.com. Enter your name and other information in the required fields, and verify your email address. When your account is created, Heroku lets you upload three applications for free. The best part is that you can do all the work directly from terminal.

Next, you need to install the Heroku CLI. On a Mac you can install it with Homebrew. To install Homebrew, run the command shown in listing 1.1 in a terminal window. This installation process is described at https://brew.sh/.

Listing 1.1. Installing Homebrew on Unix computers in terminal
/usr/bin/ruby -e "$(curl -fsSL
  https://raw.githubusercontent.com/Homebrew/install/master/
  install)"                                                   1

  • 1 Run install command in terminal window

Run brew install heroku/brew/heroku or download the installer at https://devcenter.heroku.com/articles/heroku-cli#macos. For Windows, you can find an installer at https://devcenter.heroku.com/articles/heroku-cli#windows. Linux users can install the Heroku CLI by running sudo wget -q0- https://toolbelt.heroku.com/install-ubuntu.sh | sh in terminal. If you use the graphical installer, you can step through the default settings and prompts.

When the Heroku CLI is set up, you can use the heroku keyword in terminal. The last part of this setup process is logging in to your Heroku account from terminal. Enter heroku login and then enter the email address and password you used to set up your Heroku account. You’re prepared to deploy to Heroku.

1.4. Working with the Node.js REPL in terminal

In this section, you begin using Node.js from terminal through the Node.js REPL environment. The interactive Node.js shell is the Node.js version of Read-Evaluate-Print Loop (REPL). This shell is a space in which you can write pure JavaScript and evaluate your code in the terminal window in real time. Within the window, your written code is read and evaluated by Node.js, with results printed back to your console. In this section, I look at a few things you can do in REPL.

You’ve already used terminal to check whether Node.js was installed correctly. Another way to see whether the installation was successful is to type node and press the Enter key. This action places you in the interactive Node.js shell. You’ll know that this command is successful when you see the terminal prompt change to >. To exit this prompt, type .exit or press Ctrl-C twice.

Several keywords specific to Node.js allow your terminal and REPL environment to understand when you’re running a Node.js command. In appendix A, I discuss keywords in Node.js and how they pertain to application development.

Note

If you need more practice with terminal commands, look at Part 2 of Learn Linux in a Month of Lunches by Steven Ovadia (Manning, 2016).

You can get to REPL by entering the node keyword in your terminal window without any text to follow. When you’re prompted by >, you can enter a command in JavaScript. Although this environment is reserved for testing and sandbox code, the node shell can offer a lot of benefits in development. You can enter and evaluate simple mathematical expressions, for example, or you can execute full JavaScript statements. You can also store values in variables and instantiate objects from your own custom class here. See listing 1.2 for some example REPL interactions.

In these code examples, I demonstrate some of the JavaScript ES6 syntax that appears throughout the book. In addition to the basic arithmetic I run in the REPL shell, I set up a variable with the let keyword. This keyword allows me to define a variable that’s scoped to a code block. The blocks include function blocks, to which var-defined variables are scoped, as well as conditional blocks and loops.

I also use the new class syntax to define an object. The syntax here resembles that of object-oriented programming languages but mainly acts as a wrapper over the existing JavaScript prototype structure.

Listing 1.2. REPL command examples
$ node                             1
>
> 3 + 3                            2
6
> 3 / 0
Infinity
> console.log("Hello, Universe!"); 3
Hello, Universe!
> let name = "Jon Wexler";
> console.log(name);
Jon Wexler
> class Goat {                     4
  eat(foodType) {
    console.log(`I love eating ${foodType}`);
  }
}

> let billy = new Goat();
> billy.eat("tin cans");
I love eating tin cans

  • 1 Enter REPL.
  • 2 Perform basic commands and expressions.
  • 3 Log messages to the console.
  • 4 Create ES6 classes and instantiate objects.

In the REPL environment, you have access to all the core modules that come with Node.js. Core modules are JavaScript files that come with your Node.js installation. I talk more about modules in unit 1. You’ll soon see in your own custom applications that you need to import some modules to use them in REPL. For a short list of commands to use in REPL, see table 1.1.

Table 1.1. REPL commands to remember

REPL command

Description

.break (or .clear) Exits a block within the REPL session, which is useful if you get stuck in a block of code
.editor Opens an internal editor for you to write multiple lines of code. ctrl-d saves and quits the editor
.exit Quits the REPL session
.help Lists other commands and useful tips to help you feel comfortable with this interactive shell environment
.load Followed by a local filename; gives REPL access to that file’s code
.save Followed by a new filename of your choice; saves your REPL session’s code to a file

Explore REPL by running some JavaScript commands you know. In the next lesson, you learn how to import previously written code into REPL.

Summary

In this lesson, you installed the Atom text editor and Node.js. You also verified that your Node.js environment is ready to evaluate JavaScript code by running some commands in REPL. In the next lesson, you learn how to use Node.js and terminal to build and launch an application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.132.214