amark / gun

An open source cybersecurity protocol for syncing decentralized graph data.
https://gun.eco/docs
Other
17.93k stars 1.16k forks source link

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

Open matubu opened 5 months ago

matubu commented 5 months ago

When I attempt to import 'gun/lib/time.js' within Node.js, I encounter the following error. Here is a minimal reproducible example:

{
  "dependencies": {
    "gun": "^0.2020.1239"
  },
  "type": "module"
}
import Gun from 'gun/gun.js';
import 'gun/lib/time.js';
$ npm i
$ node example.js
Hello wonderful person! :) Thanks for using GUN, please ask for help on http://chat.gun.eco if anything takes you longer than 5min to figure out!
REDACTED/node_modules/gun/lib/time.js:3
        var ify = Gun.node.ify, u;
                           ^

TypeError: Cannot read properties of undefined (reading 'ify')
    at REDACTED/node_modules/gun/lib/time.js:3:21
    at Object.<anonymous> (REDACTED/node_modules/gun/lib/time.js:138:2)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at cjsLoader (node:internal/modules/esm/translators:345:17)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:294:7)
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)

Node.js v20.10.0

This error occurs both when importing using the require and import syntax. Additionally, the error persists when attempting to import 'gun' instead of 'gun/gun.js'. However, it does not occur when running the code in the browser using a script tag to import gun as shown below:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
    <script src="./node_modules/gun/gun.js"></script>
    <script src="./node_modules/gun/lib/time.js"></script>

    <script>
        console.log(Gun().time);
    </script>
</body>
</html>
SimardeepSingh-zsh commented 5 months ago

Hello @matubu,

Thank you for reporting this issue. It seems like the 'gun/lib/time.js' module is trying to access a property of Gun.node before it's defined, hence the TypeError: Cannot read properties of undefined (reading 'ify').

One possible solution could be to ensure that Gun.node is properly initialized before importing 'gun/lib/time.js'. However, without knowing the internals of the 'gun' library, it's hard to provide a definitive solution.

As a workaround, you might want to consider importing the entire 'gun' library instead of specific modules. This should ensure that all dependencies are correctly initialized:


import Gun from 'gun';

Then, you can access the time function via Gun.time.

Please note that this is just a suggestion and might not work depending on the specifics of your project. I would also recommend reaching out to the maintainers of the ‘gun’ library for more guidance.

Sincerely, 
Simardeep Singh