{"id":18486916,"url":"https://github.com/sanathkr/go-npm","last_synced_at":"2025-04-08T20:30:25.705Z","repository":{"id":23848863,"uuid":"99657076","full_name":"sanathkr/go-npm","owner":"sanathkr","description":"Distribute and install Go binaries via NPM","archived":false,"fork":false,"pushed_at":"2024-03-24T21:45:53.000Z","size":16,"stargazers_count":173,"open_issues_count":16,"forks_count":55,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-17T23:12:48.214Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sanathkr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-08-08T06:15:47.000Z","updated_at":"2025-01-21T20:48:14.000Z","dependencies_parsed_at":"2024-06-18T13:56:54.362Z","dependency_job_id":"77e8586d-ff92-4bf7-9225-4948fbedd036","html_url":"https://github.com/sanathkr/go-npm","commit_stats":{"total_commits":16,"total_committers":2,"mean_commits":8.0,"dds":0.0625,"last_synced_commit":"ff6d9226599496f77a5065565c0c4fb55ab97843"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanathkr%2Fgo-npm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanathkr%2Fgo-npm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanathkr%2Fgo-npm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sanathkr%2Fgo-npm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sanathkr","download_url":"https://codeload.github.com/sanathkr/go-npm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247922537,"owners_count":21018815,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T12:49:57.389Z","updated_at":"2025-04-08T20:30:25.463Z","avatar_url":"https://github.com/sanathkr.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"## Go NPM\n### Distribute cross-platform Go binaries via NPM\n\nApplications 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? \n\n**Use NPM to distribute cross-platform Go binaries**\n\n## Kidding me! Why NPM?\n* **Cross-platform**: NPM is the only popular package manager that works cross-platform. \n* **Lower barier to entry**: Most developers have NPM installed already. \n* **Pain free publishing**: It just takes one command to publish - `npm publish`\n* **Dead simple install \u0026 update story**: `npm install/update -g your-awesome-app`\n* **Adds $PATH**: NPM will automatically add your binary location to $PATH and generate .cmd file for Windows. Your app just works after installation! \n\n## Okay, tell me how?\n### 1. Publish your binaries\nSetup 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.\n\nI like to use [GoReleaser](https://github.com/goreleaser/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:\n\n```\n# .goreleaser.yml\n# Build customization\nbuilds:\n  - binary: drum-roll\n    goos:\n      - windows\n      - darwin\n      - linux\n    goarch:\n      - amd64\n```\n\n`go-npm` will pull the appropriate binary for the platform \u0026 architecture where the package is being installed.\n\n### 2. Create a package.json file for your NPM app\nTo 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` 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.\n\n**Create package.json**\n\n`$ npm init`\n\nAnswer the questions to create an initial package.json file\n\n**Add go-npm dependency**\n\nFrom the directory containing package.json file, do\n\n`$ npm install go-npm --save`\n\nThis will install go-npm under to your package.json file. It will also create a `node_modules` directory where the `go-npm` package is downloaded. You don't need this directory since you are only going to publish the module and not consume it yourself. Let's go ahead and delete it.\n\n`$ rm -r node_modules`\n\n**Add postinstall and preuninstall scripts**\nHere is the magic: You ask to run `go-npm install` after it completes installing your package. This will pull down binaries from Github or Amazon S3 and install in NPM's `bin` directory. Binaries under bin directory are immediately available for use in your Terminal.\n\nEdit `package.json` file and add the following:\n```\n{\n    \"postinstall\": \"go-npm install\",\n    \"preuninstall\": \"go-npm uninstall\",\n}\n```\n\n`go-npm uninstall` simply deletes the binary from `bin` directory before NPM uninstalls your package.\n\n**Configure your binary path**\n\nYou need to tell `go-npm` where to download the binaries from, and where to install them. Edit `package.json` file and add the following configuration.\n\n```\n\"goBinary\": {\n      \"name\": \"command-name\",\n      \"path\": \"./bin\",\n      \"url\": \"https://github.com/user/my-go-package/releases/download/v{{version}}/myGoPackage_{{version}}_{{platform}}_{{arch}}.tar.gz\"\n```\n\n* *name*: Name of the command users will use to run your binary. \n* *path*: Temporary path where binaries will be downloaded to\n* *url*: HTTP Web server where binaries are hosted.\n\nFollowing variables are available to customize the URL:\n* `{{version}}`: Version number read from  `package.json` file. When you publish your package to NPM, it will use this version number. Ex: 0.0.1\n* `{{platform}}`: `$GOOS` value for the platform\n* `{{arch}}`: `$GOARCH` value for the architecture\n\nIf you use `goreleaser` to publish your modules, it will automatically set the right architecture \u0026 platform in your URL.\n\n**Publish to NPM**\n\nAll that's left now is publish to NPM. As I promised before, just one command\n\n`$ npm publish`\n\n### 3. Distribute to users\n\nTo install:\n\n`npm install -g your-app-name`\n\nTo Update:\n\n`npm update -g your-app-name`\n\n\n---\n\nWith ❤️ to the community by [Sanath Kumar Ramesh](http://twitter.com/sanathkr_)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsanathkr%2Fgo-npm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsanathkr%2Fgo-npm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsanathkr%2Fgo-npm/lists"}