From f6e7b495891b8233500b8c2bf0ac0151f2162455 Mon Sep 17 00:00:00 2001 From: bing Date: Thu, 11 Dec 2025 16:01:35 +0800 Subject: [PATCH 1/2] fix: allow undefined symbols during build time Previously, building would fail because zig expects the napi symbols during link time when they actually only exist during runtime. This PR relaxes this with the `allow-shlib-undefined` option (see: [`ld` docs](https://www.man7.org/linux/man-pages/man1/ld.1.html)). --- build.zig | 5 +++-- package.json | 2 +- zbuild.zon | 1 + 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/build.zig b/build.zig index f2f4deb..133bf7e 100644 --- a/build.zig +++ b/build.zig @@ -27,6 +27,7 @@ pub fn build(b: *std.Build) void { .linkage = .dynamic, }); + lib_example.linker_allow_shlib_undefined = true; const install_lib_example = b.addInstallArtifact(lib_example, .{ .dest_sub_path = "example.node", }); @@ -40,7 +41,7 @@ pub fn build(b: *std.Build) void { const test_napi = b.addTest(.{ .name = "napi", .root_module = module_napi, - .filters = &[_][]const u8{}, + .filters = b.option([][]const u8, "napi.filters", "napi test filters") orelse &[_][]const u8{}, }); const install_test_napi = b.addInstallArtifact(test_napi, .{}); const tls_install_test_napi = b.step("build-test:napi", "Install the napi test"); @@ -54,7 +55,7 @@ pub fn build(b: *std.Build) void { const test_example = b.addTest(.{ .name = "example", .root_module = module_example, - .filters = &[_][]const u8{}, + .filters = b.option([][]const u8, "example.filters", "example test filters") orelse &[_][]const u8{}, }); const install_test_example = b.addInstallArtifact(test_example, .{}); const tls_install_test_example = b.step("build-test:example", "Install the example test"); diff --git a/package.json b/package.json index 28845a2..1fdfac3 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "license": "MIT", "version": "0.1.1", "description": "A tool for managing and publishing Zig NAPI packages.", - "type": "module", + "type": "commonjs", "scripts": { "build": "tsc --outDir js" }, diff --git a/zbuild.zon b/zbuild.zon index 212d315..361236f 100644 --- a/zbuild.zon +++ b/zbuild.zon @@ -18,6 +18,7 @@ .imports = .{.napi}, }, .linkage = .dynamic, + .linker_allow_shlib_undefined = true, .dest_sub_path = "example.node", }, }, From a6cac0e4ec8491f967d75a249ed8949d07f536ff Mon Sep 17 00:00:00 2001 From: bing Date: Fri, 12 Dec 2025 09:10:54 +0800 Subject: [PATCH 2/2] use ES module instead --- example/test.js | 5 ++++- package.json | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/example/test.js b/example/test.js index 8f559fc..f2a2740 100644 --- a/example/test.js +++ b/example/test.js @@ -1,4 +1,7 @@ +import { createRequire } from 'node:module'; + +const require = createRequire(import.meta.url); const example = require('../zig-out/lib/example.node'); console.log(example.add(1, 2)); -console.log(example.surprise()); \ No newline at end of file +console.log(example.surprise()); diff --git a/package.json b/package.json index 1fdfac3..28845a2 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "license": "MIT", "version": "0.1.1", "description": "A tool for managing and publishing Zig NAPI packages.", - "type": "commonjs", + "type": "module", "scripts": { "build": "tsc --outDir js" },