How to publish your NodeJS application on Homebrew

Learn how to publish NodeJS CLI application on the popular Homebrew package manager using Github

Homebrew is the de-facto package manager when it comes to macOS. It is quite easy to publish your projects on Homebrew without any ruby knowledge.

Preparing the binaries

The first step is to create a standalone executable binary where the NodeJS runtime will be baked inside the binary file. For this, I made use of an awesome tool pkg by Vercel.

npm install pkg -g
pkg package.json
"pkg": {
"targets": [
"node14-linux-x64",
"node14-macos-x64",
"node14-win-x64"
],
"outputPath": "release"
}
<root>/release

Creating Tar File

Homebrew expects a TAR archive, and it’s easy to create one using the commands: In my case

cd release
mv getfilesize-cli-macos getfilesize
tar -czf getfilesize.tar.gz getfilesize
shasum -a 256 getfilesize.tar.gz

Creating GitHub Releases

Homebrew requires a URL to download your binary. There are plenty of ways to host your executable, but for my project, I used GitHub Releases, mainly because it’s free, easy to use, and my project is hosted on it.

https://github.com/bharathvaj-ganesan/getfilesize-cli/releases/download/v1.0.1/getfilesize.tar.gz

Preparing the GitHub Repository

Homebrew offers a way to create third-party repositories via Taps. In a nutshell, Taps are just GitHub repositories with specific names and a few configuration files.

homebrew-<projectname>

Creating the Formulae

Please clone the homebrew-getfilesize repository created in the previous step in your local machine. Now you will need to create a Homebrew Formula for your project, a very simple Ruby file containing the instructions to install your binary on the user computers.

Formula/
- getfilesize.rb
- README.md
class Getfilesize < Formula
desc "Get Human Readable file size information. - CLI"
homepage "https://github.com/bharathvaj-ganesan/getfilesize-cli"
url "https://github.com/bharathvaj-ganesan/getfilesize-cli/releases/download/v1.0.1/getfilesize.tar.gz"
sha256 "6c03100f5b1f8e61f5d49efab759a2efcdbcc4ac2677fcf98e7317ec660e6994"
license "MIT"
version "1.0.1"

def install
bin.install "getfilesize"
end
end
def install
bin.install "getfilesize"
end

Installing the Package

All right, if you are with me till here, your users are ready to install the package using Homebrew:

brew tap bharathvaj-ganesan/getfilesize
brew install getfilesize

Next Steps

This was a very basic way to publish your package on Homebrew, but things can get much better. In fact, we can automate the packaging process via Github Actions. DM me if you would like to discuss more on the setup.

Reference

Fullstack JavaScript Developer | Cybernaut | InfoSec Enthusiast | https://bharathvaj.me

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store