ES Modules and Cloudflare Workers
H i there, I would like to know if Cloudflare Workers are compatible with ES Modules. I have tried to deploy a project of mine with Wrangler while using imports and It gives tons of errors. I would like to get some more info on this. Any help would be muchly appreciated.
25 Replies
ES modules are the recommended format for Workers
What errors did you get?
hey kian
@kian
im importing Webtorrent library.
import WebTorrent from 'webtorrent';
Those are Node modules, which aren't available on Workers.
oh...
like not at all?
impossible?
You can use packages from NPM, as long as they don't rely on Node-only packages(like
crypto
, zlib
, etc.)But I want to use webtorrent
In some cases you can improve compatibility with the
nodejs_compat
flag, but in this case, I still don't think it would be supportedOof.
So it’s impossible?
Using WebTorrent protocol? Probably not. This specific package? You would probably have to rewrite a lot of it yourself
Well..
Wait
I once used that nodejs compat flag
Or well, if the first part wasn't clear, I'm not saying that there isn't a package that can support WebTorrent on Workers
But I got some global xmlhttprequest error
Yeah, Workers only have Fetch
Could I change it I’m the webtorrent library and possibly fix it?
Maybe? But then you would at least have to rewrite it to use CF's TCP Sockets, etc.
@HardlySpookin’
Yeah, you can only use Fetch
ok
look here
what can i do here to make it compatible
but not break it
it actually only happens in this file
i looked
I'm not sure you can? The rest of your code probably requires XHMLHttpRequest quirks to get it to work
nop
only mention
Does any part of your code reference xhr?
no
just webtorrent
Yeah, thats what I mean
its not my code
well
kinda
nevermind
sorry
If any code that your code relies on requires XMLHttpRequest, then it won't work