In this blog you will learn how to set up Node.js programming environment on your local macOS machine using Homebrew and you will be learning how to test your environment out by writing a simple Node.js program.
You will need a macOS computer running High Sierra or higher with administrative access and an internet connection.
The use of macOS Terminal
You need to use the command line , in order to install Node.js and run various commands related to developing Node.js applications. The command line is a non-graphical way to interact with your computer. Instead of clicking buttons with your mouse, you will have to type commands as text and then you will receive text-based feedback. The command line, also known as a shell, lets you automate many tasks you do on your computer daily, and is an essential tool for software developers.
To run the command line interface, you will have to use the Terminal application that has been provided by macOS. Like any other application, you will find it by going into Finder, navigating to the Applications folder, and then into the Utilities folder. From here, you will have to double-click on the Terminal application to open it up. You can even use Spotlight as an alternative by holding down the COMMAND key and pressing SPACE to find Terminal by typing it out in the box that appears.
After you have the Terminal running, let’s go on to the next level that is the installing of some prerequisites we’ll need for Node.js.
Installing Xcode’s Command Line Tools
Xcode is an integrated development environment (IDE) that is comprised of software development tools for macOS. You won’t need Xcode to write Node.js programs, but Node.js and some of its components will rely on Xcode’s Command Line Tools package.
Execute this command in the Terminal to download and install these components:
$ xcode-select --install
You’ll be prompted to start the installation, and then prompted again to accept a software license. Then the tools will download and install automatically.
We’re now ready to install the package manager Homebrew, which will let us install the latest version of Node.js.
Installing and Setting Up Homebrew
While the command line interface on macOS has a lot of the functionality you’d find in Linux and other Unix systems, it does not ship with a good package manager. A package manager is a collection of software tools that work to automate software installations, configurations, and upgrades. They keep the software they install in a central location and can maintain all software packages on the system in formats that are commonly used. Homebrew is a free and open-source software package managing system that makes the installation of software on macOS easy. We’ll use Homebrew to install the most recent version of Node.js.
To install Homebrew, type this command into your Terminal window:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
The command uses curl to download the Homebrew installation script from Homebrew’s Git repository on GitHub.
Let’s walk through the flags that are associated with the curl command:
- The -f or –fail flag tells the Terminal window to give no HTML document output on server errors.
- The -s or –silent flag mutes curl so that it does not show the progress meter, and combined with the -S or –show-error flag it will ensure that curl shows an error message if it fails.
- The -L or –location flag will tell curl to handle redirects. If the server reports that the requested page has moved to a different location, it’ll automatically execute the request again using the new location.
Once curl downloads the script, it’s then executed by the Ruby interpreter that ships with macOS, starting the Homebrew installation process.
The installation script will explain what it will do and will prompt you to confirm that you want to do it. This helps you know exactly what Homebrew is going to do to your system before you let it proceed. It also ensures you have the prerequisites in place before it continues.
You will be prompted to enter your password during the process. However, when you type your password, your keystrokes will not display in the Terminal window. This is a security measure and is something you’ll see often when prompted for passwords on the command line. Even though you don’t see them, your keystrokes are being recorded by the system, so press the RETURN key once you’ve entered your password.
Press the letter y for “yes” whenever you are prompted to confirm the installation.
Now let’s verify that Homebrew is set up correctly. Execute this command:
$ brew doctor
If there are no updates required at this time, you’ll see this in your Terminal:
Output Your system is ready to brew.
Or else you may get a warning to run another command such as brew update to ensure that your installation of Homebrew is up to date.
Now as your Homebrew is installed, you can install Node.js.
Now as you have Homebrew installed, you can install a wide range of software and developer tools and you will have to use it to install Node.js and its dependencies.
You can use Homebrew to search for everything you can install with the brew search command, but to provide us with a shorter list, let’s instead search for packages related to Node.js:
$ brew search nodejs
Now you will see a list of packages you can install , like this:
Output ==> Formulae node.js nodejs
Both of these packages will help you to install Node.js on your system. They both exist just in case you can’t remember if you need to use nodejs or node.js.
Then you need to this command to install the nodejs package:
$ brew install nodejs.
You will get to see output similar to the following in your Terminal. Homebrew will install many dependencies, but will eventually download and install Node.js itself:
Output ==> Installing dependencies for node: icu4c ==> Installing node dependency: icu4c ==> Installing node ==> Downloading https://homebrew.bintray.com/bottles/node-11.0.0.sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring node-11.0.0.sierra.bottle.tar.gz ... ==> Summary 🍺 /usr/local/Cellar/node/11.0.0: 3,936 files, 50.1MB
Homebrew installs a few related tools, including npm, which makes it easy to install and update Node.js libraries and packages you might use in your own projects.
In addition to Node.js itself .To check the version of Node.js that you installed, type :
$ node –v
This will provide output the specific version of Node.js that is currently installed, which will by default be the most up-to-date stable version of Node.js that is available.
Check the version of npm with
$ npm –v
You’ll see the version displayed:
Then you will have to use npm to install additional components, libraries, and frameworks.
To update your version of Node.js, you can first update Homebrew to get the latest list of packages, and then upgrade Node.js itself:
$ brew update $ brew upgrade nodejs
Now that Node.js is installed, let’s write a program which will ensure that every thing will work.
Creating a Simple Program
Now let us start creating a simple “Hello, World” program. This will make sure that our environment is working and gets you comfortable creating and running a Node.js program.
For executing this you need to create a new file called hello.js using nano:
$ nano hello.js
Type the following code into the file:
let message = "Hello, World!"; console.log(message);
then you will have to exit the editor by pressing CTRL+X. Then press y when prompted to save the file. You’ll be returned to your prompt.
Then run the program with the following command:
$ node hello.js
The program executes and displays its output to the screen:
Output Hello, World!
This simple program proves that you have a working development environment which you can use to continue exploring Node.js and build larger, more interesting projects.
Through this blog you have now learned to successfully install Node.js, npm, and tested out your setup by establishing and running a simple program. You can now use this to develop client-side apps or server-side apps.
Header Image Source: https://bit.ly/2Qs01yS