Closed kevglass closed 5 months ago
It would also be very helpful to us here at Streamr if a CommonJS version of node-datachannel was available!
@kevglass here is our current hack how to use node-datachannel in our CommonJS typescript project:
// synchronous type import
type NodeDataChannel = typeof import('node-datachannel', { with: { "resolution-mode": "import" } })
// declare a global for the dynamic import
declare global {
var nodeDataChannel: NodeDataChannel
}
// do this once in some async init function of your code
global.nodeDataChannel = await import('node-datachannel')
// now you can use node-datachannel like this in the rest of your code
let connection: NodeDataChannel.PeerConnection | undefined
connection = new nodeDataChannel.PeerConnection(nodeId)
Hey @ptesavol - really appreciate the solution/hack, I'll try it out in the code base and see how we go.
I think some kind of this will work. Could you please check and tell me, if is it ok? https://github.com/murat-dogan/node-datachannel/pull/240
@murat-dogan Yes, we checked the pr, and it seems to work in CommonJS now
@murat-dogan Worked for me.
Merged
I'm currently trying to integrate into an existing code base thats very stuck on CommonJS.
I've worked round the initial round of issues in Jest testing by using babel to convert to CommonJS, remove import.meta etc.
However, I'm now hitting another issues at runtime with a
Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/node-datachannel/lib/index.js from ...
I'm on Node.js v18.19.0 so I thought this would all be plain sailing.
It'd be really great to have a build that could be use in CJS as well as ECMS. Happy to have a go at it if thats any help.