To integrate go-npm into your application, you must configure your package.json with specific lifecycle scripts and a goBinary configuration object. This tells NPM to trigger the binary download and installation process automatically.
1. Add Lifecycle Scripts
Add postinstall and preuninstall scripts to your package.json to ensure binaries are downloaded upon installation and cleaned up upon uninstallation:
{
"postinstall": "go-npm install",
"preuninstall": "go-npm uninstall"
}
2. Define the goBinary Configuration
The goBinary object specifies how to locate and install your binaries. Add this to your package.json:
"goBinary": {
"name": "command-name",
"path": "./bin",
"url": "https://github.com/user/my-go-package/releases/download/v{{version}}/myGoPackage_{{version}}_{{platform}}_{{arch}}.tar.gz"
}
Configuration Keys
name: The command name users will type in their terminal to run your binary.path: The temporary local directory where binaries are downloaded before being moved to NPM's bin directory.url: The template URL for your hosted binaries. Use the following placeholders:{{version}}: The version number from your package.json.{{platform}}: The target OS (e.g., windows, darwin, linux).{{arch}}: The target architecture (e.g., amd64, arm64).
{
"postinstall": "go-npm install",
"preuninstall": "go-npm uninstall",
"goBinary": {
"name": "command-name",
"path": "./bin",
"url": "https://github.com/user/my-go-package/releases/download/v{{version}}/myGoPackage_{{version}}_{{platform}}_{{arch}}.tar.gz"
}
}