Skip to content

fix: resolve Gun.node and prevent TypeError when importing lib/time.js in Node.js (#1360) - #1439

Open
Akhil-Binu wants to merge 1 commit into
amark:masterfrom
Akhil-Binu:fix/time-node-import
Open

Akhil-Binu wants to merge 1 commit into
amark:masterfrom
Akhil-Binu:fix/time-node-import

Conversation

@Akhil-Binu

Copy link
Copy Markdown

Summary of Changes

Fixes #1360.

Problem

When importing or requiring gun/lib/time.js in a Node.js environment (CommonJS or ESM):

require('gun/lib/time');
// or
import 'gun/lib/time.js';

Node throws a fatal runtime error:

TypeError: Cannot read properties of undefined (reading 'ify')
    at lib/time.js:3:21

Root Cause

In gun.js, the backwards-compatibility block (lines 2095–2350) that exports Gun.node, Gun.state, Gun.val, and related internal compatibility helpers begins with:

;(function(){
	var u;
	if(''+u == typeof Gun){ return }

In browser environments, window.Gun is bound to the window object earlier, so typeof Gun !== 'undefined' and the block executes. However, in Node.js environments, Gun is local to the primary IIFE and exported via module.exports = Gun; without defining a global Gun. Therefore, in Node.js typeof Gun evaluates to 'undefined', causing the compatibility block to return early without attaching Gun.node to the exported Gun instance.

Solution

  1. gun.js: Update the compatibility block entry check to resolve Gun from module.exports when running in Node.js environments:
    var Gun = (typeof window !== 'undefined'? window.Gun : (typeof module !== 'undefined'? module.exports : null)) || (typeof Gun !== 'undefined'? Gun : null);
    var u;
    if(!Gun){ return }
  2. lib/time.js: Add a safe defensive fallback for ify node formatting:
    var ify = (Gun.node && Gun.node.ify) || function(n, s){ return (n = n || {}, n._ = (n._ || {}), n._['#'] = (typeof s === 'string'? s : (s && s.soul)), n) }, u;
  3. gun.min.js: Rebuilt with npm run minify.
  4. test/time.js: Added automated Mocha tests verifying error-free importing in Node.js and time graph chaining.

Verification

  • Ran full test suite via mocha: 173 passing, 0 regressions.
  • Verified both CommonJS (require('gun/lib/time')) and ESM imports without errors.

…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
@ToniAdreal

Copy link
Copy Markdown

Verified independently against current master (6b72bd1) and this PR head (7ad8d7f) with Node v24.22.1:

Bug reproduces on master — both module systems fail identically:

$ node -e "require('gun/lib/time')"
TypeError: Cannot read properties of undefined (reading 'ify')
    at lib/time.js:3:21

Exit code 1. Same error via ESM import './lib/time.js'.

Fix works on the PR head — both import cleanly (exit code 0), and typeof Gun.node.ify === 'function', typeof Gun.chain.time === 'function'.

Tests: the two new tests in test/time.js pass (mocha test/time.js — 2 passing, 0 failing).

Minified bundle: rebuilt locally with npm run minify (uglifyjs) — output is byte-identical to the committed gun.min.js (45,366 bytes).

Two notes from the same check:

  • The gun.js compat-block change also unblocks lib/space.js, which has the same top-level Gun.node.ify read (line 3) and failed on master with the same TypeError, but imports cleanly on this head with no change to space.js itself. The fix's scope is slightly wider than time.js alone.
  • lib/role.js still fails to import with window is not defined on both master and this head — pre-existing and unrelated to this PR, just noting it so it isn't mistaken for a regression.

Full suite: default mocha run on this head — 173 passing, 11 pending, 0 failing (52s), matching the PR description exactly. For comparison, the same suite on master (6b72bd1) in the same environment gives 73 passing and 109 failing — the compat-block change is what makes the difference, since Gun.node/Gun.val/Gun.text/Gun.obj/Gun.list/Gun.graph are all undefined on master in Node and all defined on this head.

Disclosure: I used Muse to help locate the relevant code paths and run the reproduction; the verification steps and conclusions above are my own.

This branch has not been deployed

No deployments
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.

[BUG] Error when importing 'gun/lib/time.js' in nodejs

2 participants