How to Install Node.js and Create a Local Development Environment on macOS

How to Install Node.js and Create a Local Development Environment on macOS

In this tutorial, you'll set up a Node.js programming environment on your local macOS machine using Homebrew, and you'll test your environment out by writing a simple Node.js program.



Introduction

Node.js is an open source JavaScript runtime environment for easily building server-side applications. It’s also the runtime that powers many client-side development tools for modern JavaScript frameworks.

In this tutorial, you’ll set up a Node.js programming environment on your local macOS machine using Homebrew, and you’ll test your environment out by writing a simple Node.js program.


Prerequisites

You will need a macOS computer running High Sierra or higher with administrative access and an internet connection.


Step 1 — Using the macOS Terminal

You’ll use the command line 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’ll type commands as text and 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 access the command line interface, you’ll use the Terminal application provided by macOS. Like any other application, you can find it by going into Finder, navigating to the Applications folder, and then into the Utilities folder. From here, double-click the Terminal application to open it up. Alternatively, you can use Spotlight by holding down the COMMAND key and pressing SPACE to find Terminal by typing it out in the box that appears.

If you’d like to get comfortable using the command line, take a look at An Introduction to the Linux Terminal. The command line interface on macOS is very similar, and the concepts in that tutorial are directly applicable.

Now that you have the Terminal running, let’s install some prerequisites we’ll need for Node.js.


Step 2 — 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.


Step 3 — 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 simplifies the installation of software on macOS. 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 lets 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’ll 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 no updates are required at this time, you’ll see this in your Terminal:

OutputYour system is ready to brew.

Otherwise, 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 that Homebrew is installed, you can install Node.js.


Step 4 — Installing Node.js

With Homebrew installed, you can install a wide range of software and developer tools. We’ll 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

You’ll see a list of packages you can install, like this:

Output==> Formulae
node.js
nodejs

Both of these packages 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.

Execute this command to install the nodejs package:

brew install nodejs

You’ll 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

In addition to Node.js itself, 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.

To check the version of Node.js that you installed, type

node -v

This will 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.

Outputv11.0.0

Check the version of npm with

npm -v

You’ll see the version displayed:

Output6.4.1

You’ll 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 to ensure everything works.


Step 5 — Creating a Simple Program

Let’s create 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.

To do this, create a new file called hello.js using nano:

nano hello.js

Type the following code into the file:

hello.js

let message = "Hello, World!";
console.log(message);

Exit the editor by pressing CTRL+X. Then press y when prompted to save the file. You’ll be returned to your prompt.

Now run the program with the following command:

node hello.js

The program executes and displays its output to the screen:

OutputHello, World!

This simple program proves that you have a working development environment. You can use this environment to continue exploring Node.js and build larger, more interesting projects.


Conclusion

You’ve successfully installed Node.js, npm, and tested out your setup by creating and running a simple program. You can now use this to develop client-side apps or server-side apps. T


Learn More

Angular and Nodejs Integration Tutorial

Build a Basic CRUD App with Node and React

Node.js - Express Persistent Session Store with PostgreSQL + Sequelize

Secure Node.js, Express.js and PostgreSQL API using Passport.js

Machine Learning In Node.js With TensorFlow.js

Full Stack Developers: Everything You Need to Know

MEAN Stack Tutorial MongoDB, ExpressJS, AngularJS and NodeJS

The Complete Node.js Developer Course (2nd Edition)

Learn and Understand NodeJS

Angular (Angular 2+) & NodeJS - The MEAN Stack Guide

Originally published by Brian Hogan at https://www.digitalocean.com

Top 7 Most Popular Node.js Frameworks You Should Know

Top 7 Most Popular Node.js Frameworks You Should Know

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser. In this post, you'll see top 7 of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).

Node.js is an open-source, cross-platform, runtime environment that allows developers to run JavaScript outside of a browser.

One of the main advantages of Node is that it enables developers to use JavaScript on both the front-end and the back-end of an application. This not only makes the source code of any app cleaner and more consistent, but it significantly speeds up app development too, as developers only need to use one language.

Node is fast, scalable, and easy to get started with. Its default package manager is npm, which means it also sports the largest ecosystem of open-source libraries. Node is used by companies such as NASA, Uber, Netflix, and Walmart.

But Node doesn't come alone. It comes with a plethora of frameworks. A Node framework can be pictured as the external scaffolding that you can build your app in. These frameworks are built on top of Node and extend the technology's functionality, mostly by making apps easier to prototype and develop, while also making them faster and more scalable.

Below are 7of the most popular Node frameworks at this point in time (ranked from high to low by GitHub stars).

Express

With over 43,000 GitHub stars, Express is the most popular Node framework. It brands itself as a fast, unopinionated, and minimalist framework. Express acts as middleware: it helps set up and configure routes to send and receive requests between the front-end and the database of an app.

Express provides lightweight, powerful tools for HTTP servers. It's a great framework for single-page apps, websites, hybrids, or public HTTP APIs. It supports over fourteen different template engines, so developers aren't forced into any specific ORM.

Meteor

Meteor is a full-stack JavaScript platform. It allows developers to build real-time web apps, i.e. apps where code changes are pushed to all browsers and devices in real-time. Additionally, servers send data over the wire, instead of HTML. The client renders the data.

The project has over 41,000 GitHub stars and is built to power large projects. Meteor is used by companies such as Mazda, Honeywell, Qualcomm, and IKEA. It has excellent documentation and a strong community behind it.

Koa

Koa is built by the same team that built Express. It uses ES6 methods that allow developers to work without callbacks. Developers also have more control over error-handling. Koa has no middleware within its core, which means that developers have more control over configuration, but which means that traditional Node middleware (e.g. req, res, next) won't work with Koa.

Koa already has over 26,000 GitHub stars. The Express developers built Koa because they wanted a lighter framework that was more expressive and more robust than Express. You can find out more about the differences between Koa and Express here.

Sails

Sails is a real-time, MVC framework for Node that's built on Express. It supports auto-generated REST APIs and comes with an easy WebSocket integration.

The project has over 20,000 stars on GitHub and is compatible with almost all databases (MySQL, MongoDB, PostgreSQL, Redis). It's also compatible with most front-end technologies (Angular, iOS, Android, React, and even Windows Phone).

Nest

Nest has over 15,000 GitHub stars. It uses progressive JavaScript and is built with TypeScript, which means it comes with strong typing. It combines elements of object-oriented programming, functional programming, and functional reactive programming.

Nest is packaged in such a way it serves as a complete development kit for writing enterprise-level apps. The framework uses Express, but is compatible with a wide range of other libraries.

LoopBack

LoopBack is a framework that allows developers to quickly create REST APIs. It has an easy-to-use CLI wizard and allows developers to create models either on their schema or dynamically. It also has a built-in API explorer.

LoopBack has over 12,000 GitHub stars and is used by companies such as GoDaddy, Symantec, and the Bank of America. It's compatible with many REST services and a wide variety of databases (MongoDB, Oracle, MySQL, PostgreSQL).

Hapi

Similar to Express, hapi serves data by intermediating between server-side and client-side. As such, it's can serve as a substitute for Express. Hapi allows developers to focus on writing reusable app logic in a modular and prescriptive fashion.

The project has over 11,000 GitHub stars. It has built-in support for input validation, caching, authentication, and more. Hapi was originally developed to handle all of Walmart's mobile traffic during Black Friday.

Difference between AngularJS, React, Ember, Backbone, and Node.js.

The most common thing between all of them is that they are Single Page Apps. The SPA is a single page where much of the information remains the same and only some piece of data gets modified when you click on other categories/option.

Node.js Tutorial for Beginners | Node.js Crash Course | Node.js Certification Training

This courseis designed for professionals who aspire to be application developers and gain expertise in building real-time, highly-scalable applications in Node.js. The following professionals can go for this course :

Why learn Node.js?

Node.js uses JavaScript - a language known to millions of developers worldwide - thus giving it a much lower learning curve even for complete beginners. Using Node.js you can build simple Command Line programs or complex enterprise level web applications with equal ease. Node.js is an event-driven, server-side, asynchronous development platform with lightning speed execution. Node.js helps you to code the most complex functionalities in just a few lines of code...

Thanks for reading :heart: If you liked this post, share it with all of your programming buddies! Follow me on Facebook | Twitter

Learn More

The Complete Node.js Developer Course (3rd Edition)

Angular & NodeJS - The MEAN Stack Guide

NodeJS - The Complete Guide (incl. MVC, REST APIs, GraphQL)

Docker for Node.js Projects From a Docker Captain

Intro To MySQL With Node.js - Learn To Use MySQL with Node!

Node.js Absolute Beginners Guide - Learn Node From Scratch

React Node FullStack - Social Network from Scratch to Deploy

Selenium WebDriver - JavaScript nodeJS webdriver IO & more!

Complete Next.js with React & Node - Beautiful Portfolio App

Build a Blockchain & Cryptocurrency | Full-Stack Edition