Skip to content
napi-bindings

Search

Search pages, projects and the Node-API reference

Language bindings

napi-go

0.x

Native Node.js addons written in Go: annotated functions, structs and classes become a typed npm package.

1 star0 forksv0.1.0· Nov 12, 2026Apache-2.0
docs/getting-started.mdEdit on GitHub

Getting started

Requirements

  • Go 1.24 or later, with cgo.
  • A C compiler: gcc or clang on Linux and macOS; on Windows MinGW gcc, or zig (napi-go build -zig always).
  • Node.js 22 or later.
  • Optional: zig, to build for other platforms (napi-go build -target …) and to make Linux release binaries that run on older distributions (glibc 2.17).

Create a project

go run github.com/napi-bindings/napi-go/cmd/napi-go@latest new my-addon
cd my-addon
npm run build:debug
npm test

napi-go new creates:

File
main.go the addon: annotated Go functions and a class
go.mod requires napi-go as a library and as a tool, so go tool napi-go runs the CLI at the version of the library
package.json the npm package; napiGo holds the napi-go configuration
test/index.test.mjs tests with node:test
.github/workflows/CI.yml builds and tests every target, publishes on a version tag
.gitignore, README.md

npm run build:debug runs go tool napi-go build, which first generates zz_napi.go, index.js and index.d.ts from the annotations, then builds my-addon.<target>.node for this machine. Commit the generated files: they are part of the package.

Write a function

// Greet returns a greeting for name.
//
//napi:export
func Greet(name string) string { return "hello " + name }
npm run build:debug
node -e "console.log(require('.').greet('Ann'))"

From TypeScript and ES modules the exports have types and names:

import { greet } from 'my-addon'
greet('Ann')   // greet(name: string): string, with the Go comment as JSDoc

The guide covers the parameter and result types, errors, async functions and classes.

Build for other platforms

go tool napi-go build -release                        # this machine
go tool napi-go build -release -target linux-arm64-gnu
go tool napi-go build -release -target all            # the targets of package.json

Builds for another platform use zig as C compiler. The targets are linux-x64-gnu, linux-arm64-gnu, darwin-x64, darwin-arm64, win32-x64-gnu and win32-arm64-gnu; see the CLI reference.

Publish

The package follows the layout of napi-rs: the main package holds index.js and index.d.ts, and each target has a package my-addon-<target> with its .node file, installed by npm only on matching machines.

The workflow created by new does it all: push a tag v<version> equal to the version of package.json, with the repository secret NPM_TOKEN. By hand:

go tool napi-go artifacts -d <directory with the built .node files>
go tool napi-go prepublish -publish
npm publish

Without the template

An existing Go module can become an addon without napi-go new:

go get github.com/napi-bindings/napi-go@latest
go get -tool github.com/napi-bindings/napi-go/cmd/napi-go

Write a main package with an empty func main() {} and annotated declarations, then go tool napi-go build (without package.json the binary is named after the directory). Or skip the generator and register with napi.Export in an init function (napi package), and build with go build -buildmode=c-shared -o addon.node ..