First of all, thank you for an interesting project; I've been following Gun for some time and finally am looking into using for a project.
Loading gun via ES Modules via a script or an import throws error when setting a peer:
Script:
<script src="/gun/gun.js" type="module"></script>
<script type="module">
window.onload = e =>
const gun = new Gun('http://127.0.0.1:4977/gun')
const node = gun.get('data')
node.get('message').put('Hello, Gun!')
node.once(doc => console.log(doc.message))
}
</script>
Import:
import Gun from 'gun/gun'
const gun = new Gun('http://127.0.0.1:4977/gun')
const node = gun.get('data')
node.get('message').put('Hello, Gun!')
node.once(doc => console.log(doc.message))
Error:
Uncaught TypeError: Cannot read property 'to' of undefined
at Mesh.mesh.say (gun.js:2151)
at go (gun.js:2186)
at Object.Mesh.mesh.say [as next] (gun.js:2194)
at Object.next (gun.js:1964)
at Object.universe [as next] (gun.js:731)
at Object.onto [as on] (gun.js:205)
at gun.js:2416
I am running a Node server that uses Gun and is running at http://127.0.0.1:4977.
Gun is working just fine between browser and peer and throws no errors when loading it via a regular script.
I've tried numerous ways to get ES modules and peers working with Gun, but this error is always the result.
After doing more testing with the gun-starter-app, it looks like everything works with and without ESM; including RAD.
The version listed in gun-starter-app is "gun": "^0.9.9992".
However, a new install of gun shows "gun": "^0.2020.401", then the errors are thrown.
With regards to the bug thrown on line 2151 of gun.js:
if(this.to){ this.to.next(msg) } // compatible with middleware adapters.
I noticed that when loading gun as a script, the this on line 2151 is either an object or Window:

When loading gun as a module, this is either an object or undefined:

This could be due to modules not having a this context, while scripts do.
@joedakroub WELCOME TO THE COMMUNITY!!! 馃憦 馃憦 Thanks for reporting this.
That looks like some other bug with sent messages?
Or were you able to get this to work ? that you closed the issue ?
I'm still trying to figure out how to get magic of ESM working, could you help with this?
@amark Thank you for reaching out and the warm welcome! I've been really enjoying learning about Gun and have watched several of your videos on YouTube. Really impressed with everything you've been doing and this project.
I initially closed this issue because it wasn't working and then it was, so I closed it. However, I did more digging and found that the version of Gun used in the starter works, but the latest version of Gun does not.
When I can, I will keep digging to see what else I can discover about this. I use ESM for most of my work now (with and without build systems), so I am motivated to see this work.
Thank you again for such a great library. There truly is nothing quite like it out there!
@joedakroub thanks! Yes, please, I want to support as many environments as possible. When you figure out what is up, would you be willing to submit a PR for a test? To make sure we don't regress in the future?
@amark I forked gun to test a potential fix and integrate it with my test project.
Here is the commit: https://github.com/joedakroub/gun/commit/1a71145b4dbbc5956992902484f21c3a1c68aaff
The issue is because this is undefined when using modules, but is window inside of a script.
I just added a check for this before the function call and it removes the error.
However, I am not sure if this has any unintended consequences not knowing Gun's code very well. The unit tests still pass however.
I'd be happy to submit a PR back with this fix as well if you are in agreement with the approach.
@joedakroub interesting find... just a typo, yay!
But woah, what is going on? mesh.say gets called as a function at some point like mesh.say.call(eventContext, args) to pass scope around, why would bundle crash on this? This should be valid for both module environments & window environments.
Either way, if tests pass & if it fixes/allows ES Modules, then let's move forward. Could you submit this as a PR please?
I just gave this fix a shot locally and it seems to work for me. PR with this would be very welcome!
@joedakroub I want you to get credit for it! :) Send a PR in!! :D :) 馃憦
@amark Happy to add a PR. Adding it now. Thank you!
waah000!
closing, in expectation.
Most helpful comment
@joedakroub interesting find... just a typo, yay!
But woah, what is going on?
mesh.saygets called as a function at some point likemesh.say.call(eventContext, args)to pass scope around, why would bundle crash on this? This should be valid for both module environments & window environments.Either way, if tests pass & if it fixes/allows ES Modules, then let's move forward. Could you submit this as a PR please?