Working with Homebrew
"Homebrew is the easiest and most flexible way to install the UNIX tools Apple didn't include with macOS."
"Homebrew’s creator @mxcl was too concerned with the beer theme and didn’t consider that the project may actually prove popular. By the time Max realised that it was popular, it was too late. However, today, the first Google hit for “homebrew” is not beer related."
For system requirements, see:
https://docs.brew.sh/Installation
Previously, we installed VS Code, which is an integrated development environment. In this article, we'll use Homebrew to install Node. In a future tutorial, we'll use Homebrew to install MongoDB.
Install Homebrew
To install, visit the Homebrew site (below), then copy and paste the designated script into macOS Terminal.
Check which version of Homebrew you have installed:
brew -v
or brew --version
Example output: Homebrew 3.2.4
To see the install location: which brew
Example output: /opt/homebrew/bin/brew
"Locate a program file in the user's path."
https://ss64.com/osx/which.html
Opt-out of analytics: brew analytics off
https://docs.brew.sh/Analytics
Frequently used commands
"A formula is a package definition written in Ruby."
Install formula: brew install <name>
Uninstall formula: brew uninstall <name>
Run updates (daily): brew update
Upgrade everything: brew upgrade
For example:
"You have 1 outdated formula installed.
You can upgrade it with brew upgrade
or list it with brew outdated."
==> Upgrading 1 outdated package:
node 16.4.0 -> 16.4.1
==> Upgrading node
16.4.0 -> 16.4.1
"Check your system for potential problems."
brew doctor
Remove older versions of the installed formula:
brew cleanup
Additional documentation:
https://docs.brew.sh
Install Node with Homebrew
Node lets you run JavaScript outside of a browser. "Node.js lets developers use JavaScript to write command line tools and for server-side scripting—running scripts server-side to produce dynamic web page content before the page is sent to the user's web browser."
"In January 2010, a package manager was introduced for the Node.js environment called NPM. The package manager makes it easier for programmers to publish and share source code of Node.js packages and is designed to simplify installation, updating, and uninstallation of packages."
https://en.wikipedia.org/wiki/Node.js
"NPM (originally short for Node Package Manager) is a package manager for the JavaScript programming language maintained by npm, Inc."
When you install Node.js, you also get NPM.
"NPM is included as a recommended feature in the Node.js installer. NPM consists of a command line client that interacts with a remote registry."
https://en.wikipedia.org/wiki/Npm_(software)
For more info about using NPM, see the following:
https://docs.npmjs.com/packages-and-modules
You can install Node.js from the developer website (via the installer) or by using Homebrew.
brew install node
https://formulae.brew.sh/formula/node
https://nodejs.org/en/download/package-manager/#macos
https://docs.npmjs.com/try-the-latest-stable-version-of-node
Check Node or NPM version:
node -v
or node --version
npm -v
or npm --version
Example output: v16.5.0
for Node and 7.19.1
for NPM
You can uninstall Node (and npm) using Homebrew with the following.
https://docs.brew.sh/Manpage#uninstall-formula
brew uninstall node
Using Node
You should now be able to execute the code in a JavaScript file using Node.js. To test this out, create a JavaScript file called example.js
and add the code console.log('Hello');
. To run your example file, enter the following into Terminal on macOS: node example.js
. The command-line output should be Hello
. Take note that JavaScript is running with Node.js instead of in a browser.
Uninstall Homebrew
If you need to uninstall Homebrew, you can follow the instructions below.
"To uninstall Homebrew, run the uninstall script from the Homebrew/install repository."
https://docs.brew.sh/FAQ#how-do-i-uninstall-homebrew
https://github.com/homebrew/install#uninstall-homebrew
Donate to Homebrew
https://github.com/homebrew/brew#donations
https://www.patreon.com/homebrew
Summary
In this section, you installed Node via Homebrew and became acquainted with NPM. We'll revisit NPM in another article since there's still quite a bit more to learn.