Go NPM
Distribute cross-platform Go binaries via NPM.
🚀 Modernized version with TypeScript, comprehensive testing, and enhanced reliability
Applications written in Golang are portable - you can easily cross-compile binaries that work on Windows, Mac, and Linux. But how do you distribute the binaries to customers? When you publish new releases, how do they update the binary?
Use NPM to distribute cross-platform Go binaries
Note: This is go-npm-next, a modernized and enhanced version of the original go-npm package. See Credits for information about other versions.
Kidding me! Why NPM?
- Cross-platform: NPM is the only popular package manager that works cross-platform.
- Lower barier to entry: Most developers have NPM installed already.
- Pain free publishing: It just takes one command to publish - npm publish
- Dead simple install & update story: npm install/update -g your-awesome-app
- Adds $PATH: NPM will automatically add your binary location to $PATH and generate .cmd file for Windows. Your app just works after installation!
Okay, tell me how?
1. Publish your binaries
Setup your Go application to compile and publish binaries to a file server. This could be Github Releases or Amazon S3 or even Dropbox. All you need is a link.
I like to use GoReleaser to setup by release process. You create a simple YAML configuration file like this and run goreleaser CLI to publish binaries for various platform/architecture combination to Github:
# .goreleaser.yml
# Build customization
builds:
- binary: drum-roll
goos:
- windows
- darwin
- linux
goarch:
- amd64go-npm-next will pull the appropriate binary for the platform & architecture where the package is being installed.
2. Create a package.json file for your NPM app
To publish to NPM, you need to create a package.json file. You give your application a name, link to Readme, Github repository etc, and more importantly add go-npm-next as a dependency. You can create this file in an empty directory in your project or in a separate Git repository altogether. It is your choice.
Create package.json
$ npm init