Developing on a Mac – Part III – NodeJS

Categories:

I wrote Part I of this series to provide a foundation upon which to build a collection of minimal guides to developing software on a Mac. In this post let’s look at what needs to be installed on your Mac for developing NodeJS applications. If you haven’t read Part I, make sure you do and install:

  • Homebrew
  • Developer Tools

NodeJS

NodeJS can be installed and managing using a number of package managers, just like we saw with Ruby. Or, you can install it directly from nodejs.org. You can even install it with Homebrew!

Homebrew Install

Installing NodeJS with Homebrew is as simple as running brew install node in your terminal.

Both node (the NodeJS engine) and npm (Node Package Manager) are installed:

Now to install a few node packages in the global environment:

Where’d they get installed?

Changing versions

It’s not uncommon to be developing with multiple NodeJS versions, especially in a professional environment. Installing and managing Node versions with Homebrew isn’t too difficult, if you make the decision to only have one version installed at a time. No joke.

Recall that we installed v19.1.10. As it turns out that isn’t a Long Term Support (LTS) release. Let’s install v14.

You are undoubtedly protesting: “What about all of the cool command line tools I installed through npm?!” You should probably be keeping a list of those and reinstall them.

Using NodeJS.org

NodeJS.org hosts binary package installers for Node. You can install these packages by downloading and then double-clicking on them or via the command-line.

Installing via the command line uses the installer application:

If you’ve never used pkgutil before on your Mac, try it out!

As promised, the NodeJS.org installs node in /usr/local/bin:

Let’s see where our modules are installed: npm install -g dotenv

Oh! sudo access is required. And that’s why I’d stop right here. NodeJS installed in this manner is not going to support multiple versions and will make your development experience miserable.

Using nvm

A third method of installing NodeJS is through NVM, the Node Version Manager. Of course you can install nvm using their instructions, or use Homebrew.

Once you’ve installed a NVM you can use nvm install to install a specific version.

And installing the dotenv package.

Creating a Basic Application

With NodeJS installed, let’s take it for a spin and write a “simple” application that reads data from a Postgres database.

We’ll be using the pg, dotenv, and yargs packages.

Of course, to communicate with a Postgres database we’ll need one! Here we’ll install Postgres, start it, and then create a new database, user, and grants for this example.

Back to our Node code. We’ll put the following in index.js:

You can try running this as is, but the connection to the database will fail. That can be fixed with a .env file which utilizes the pg default environment variables for the database, username, password, etc.

Which Installation Method to Use?

 

I recommend using either Homebrew or NVM for installing and managing versions of NodeJS. Uninstalling the version provided by NodeJS.org is a pain in the ass, but can be done. I wouldn’t recommend the method
here, but rather use this method.

WARNING: sudo and rm -f ahead. Always read and get a feeling for what commands do before running them. You may consider commenting out the rm command to see what files are going to be removed.

followed by

and finally,

Leave a Reply

Your email address will not be published. Required fields are marked *