Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
matubu opened this issue Jan 23, 2024 · 1 comment
Open

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

matubu opened this issue Jan 23, 2024 · 1 comment

Comments

@matubu
Copy link

matubu commented Jan 23, 2024

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
Copy link
Contributor

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

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

No branches or pull requests

2 participants