Skip to content

fix: make generated binding loaders Node 12 compatible#1

Open
NullVoxPopuli-ai-agent wants to merge 1 commit into
NullVoxPopuli:patch-1from
NullVoxPopuli-ai-agent:node12-loader-compat
Open

fix: make generated binding loaders Node 12 compatible#1
NullVoxPopuli-ai-agent wants to merge 1 commit into
NullVoxPopuli:patch-1from
NullVoxPopuli-ai-agent:node12-loader-compat

Conversation

@NullVoxPopuli-ai-agent

Copy link
Copy Markdown

Stacked on top of napi-rs#1147 (targets the patch-1 branch, not main).

napi-rs#1147 expands the CI matrix to include Node 12. The first thing that breaks on Node 12 isn't the binding — it's the NAPI-RS-generated JS loader itself failing to parse/load, even though every package still declares engines: { "node": ">= 12" }.

Two incompatibilities, both emitted by @napi-rs/cli 3.x:

code problem on Node 12
const { readFileSync } = require('node:fs') the node: scheme in CommonJS require() is only supported on Node ≥ 14.18 / 16 → throws Cannot find module 'node:fs'
process.report?.getReport / process.config?.variables?.… optional chaining (?.) landed in Node 14 → Node 12 fails to parse the module entirely

This downlevels both to plain require('fs') and && guards across all six native binding loaders (argon2, bcrypt, crc32, jieba, jsonwebtoken, xxhash). The output still parses cleanly at ES2019 (verified with acorn ecmaVersion: 2019) and is unchanged behaviourally on newer Node.

Caveat — the WASI job

napi-rs#1147 expands the matrix on test-wasi-nodejs. WASI (wasm32-wasip1-threads) needs Node ≥ 18 to actually run, and the *.wasi.cjs loaders additionally use ?? + require('node:wasi'). So this change makes the native loaders Node-12-safe (honoring engines >= 12), but it does not make the WASI test itself pass on 12/14/16 — that job will still need its matrix narrowed to Node ≥ 18.

Note on the source

These files are auto-generated by @napi-rs/cli; the real fix belongs in its loader template (it hardcodes node:fs + ?.). This patches the committed output so the declared >= 12 support holds today.

The NAPI-RS-generated binding loaders are not parseable by Node 12,
which the packages still declare support for (engines: ">= 12"):

- `require('node:fs')` — the `node:` scheme in CommonJS `require()`
  is only supported on Node >= 14.18 / 16, so it throws on Node 12.
- `process.report?.getReport` / `process.config?.variables?.…` —
  optional chaining (`?.`) landed in Node 14, so Node 12 fails to
  parse the module entirely.

Downlevel both to plain `require('fs')` and `&&` guards so the loaders
load on every Node version the packages claim to support. Applies to
all six native binding loaders.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant