Repository navigation
fix: resolve Gun.node and prevent TypeError when importing lib/time.js in Node.js (#1360) - #1439
Akhil-Binu wants to merge 1 commit into
Conversation
…s in Node.js (amark#1360) - Fix internal compatibility block in gun.js to resolve Gun from module.exports in Node.js environments - Add safe fallback in lib/time.js for ify node formatting - Rebuild gun.min.js - Add unit tests in test/time.js verifying Node.js import and time chaining
|
Verified independently against current master ( Bug reproduces on master — both module systems fail identically: Exit code 1. Same error via ESM Fix works on the PR head — both import cleanly (exit code 0), and Tests: the two new tests in Minified bundle: rebuilt locally with Two notes from the same check:
Full suite: default Disclosure: I used Muse to help locate the relevant code paths and run the reproduction; the verification steps and conclusions above are my own. |
Summary of Changes
Fixes #1360.
Problem
When importing or requiring
gun/lib/time.jsin a Node.js environment (CommonJS or ESM):Node throws a fatal runtime error:
Root Cause
In
gun.js, the backwards-compatibility block (lines 2095–2350) that exportsGun.node,Gun.state,Gun.val, and related internal compatibility helpers begins with:In browser environments,
window.Gunis bound to the window object earlier, sotypeof Gun !== 'undefined'and the block executes. However, in Node.js environments,Gunis local to the primary IIFE and exported viamodule.exports = Gun;without defining a globalGun. Therefore, in Node.jstypeof Gunevaluates to'undefined', causing the compatibility block to return early without attachingGun.nodeto the exportedGuninstance.Solution
gun.js: Update the compatibility block entry check to resolveGunfrommodule.exportswhen running in Node.js environments:lib/time.js: Add a safe defensive fallback forifynode formatting:gun.min.js: Rebuilt withnpm run minify.test/time.js: Added automated Mocha tests verifying error-free importing in Node.js and time graph chaining.Verification
mocha: 173 passing, 0 regressions.require('gun/lib/time')) and ESM imports without errors.