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
kian
kian14mo ago
ES modules are the recommended format for Workers What errors did you get?
Mr. Goodman
Mr. GoodmanOP14mo ago
hey kian @kian im importing Webtorrent library. import WebTorrent from 'webtorrent';
X [ERROR] Build failed with 41 errors:

node_modules/bittorrent-dht/client.js:10:19: ERROR: Could not resolve "crypto"
node_modules/bittorrent-protocol/index.js:4:19: ERROR: Could not resolve "crypto"
node_modules/body-parser/lib/read.js:20:19: ERROR: Could not resolve "zlib"
node_modules/body-parser/lib/types/urlencoded.js:228:20: ERROR: Could not resolve "querystring"
node_modules/content-disposition/index.js:22:23: ERROR: Could not resolve "path"
...
X [ERROR] Build failed with 41 errors:

node_modules/bittorrent-dht/client.js:10:19: ERROR: Could not resolve "crypto"
node_modules/bittorrent-protocol/index.js:4:19: ERROR: Could not resolve "crypto"
node_modules/body-parser/lib/read.js:20:19: ERROR: Could not resolve "zlib"
node_modules/body-parser/lib/types/urlencoded.js:228:20: ERROR: Could not resolve "querystring"
node_modules/content-disposition/index.js:22:23: ERROR: Could not resolve "path"
...
kian
kian14mo ago
Those are Node modules, which aren't available on Workers.
Mr. Goodman
Mr. GoodmanOP14mo ago
oh... like not at all? impossible?
Hello, I’m Allie!
You can use packages from NPM, as long as they don't rely on Node-only packages(like crypto, zlib, etc.)
Mr. Goodman
Mr. GoodmanOP14mo ago
But I want to use webtorrent
Hello, I’m Allie!
In some cases you can improve compatibility with the nodejs_compat flag, but in this case, I still don't think it would be supported
Mr. Goodman
Mr. GoodmanOP14mo ago
Oof. So it’s impossible?
Hello, I’m Allie!
Using WebTorrent protocol? Probably not. This specific package? You would probably have to rewrite a lot of it yourself
Mr. Goodman
Mr. GoodmanOP14mo ago
Well.. Wait I once used that nodejs compat flag
Hello, I’m Allie!
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
Mr. Goodman
Mr. GoodmanOP14mo ago
But I got some global xmlhttprequest error
Hello, I’m Allie!
Yeah, Workers only have Fetch
Mr. Goodman
Mr. GoodmanOP14mo ago
Could I change it I’m the webtorrent library and possibly fix it?
Hello, I’m Allie!
Maybe? But then you would at least have to rewrite it to use CF's TCP Sockets, etc.
Mr. Goodman
Mr. GoodmanOP14mo ago
@HardlySpookin’
Uncaught TypeError: globalThis.XMLHttpRequest is not a constructor
at worker.js:28241:13 in checkTypeSupport
at worker.js:28262:40 in
C:/Users/User/AppData/Roaming/npm/node_modules/wrangler/node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/capability.js
at worker.js:16:58 in __init
at worker.js:28524:7 in
C:/Users/User/AppData/Roaming/npm/node_modules/wrangler/node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/request.js
at worker.js:16:58 in __init
at worker.js:28741:7 in node-modules-polyfills:http
at worker.js:16:58 in __init
at worker.js:28860:23 in node-modules-polyfills-commonjs:http
at worker.js:19:52 in __require2
at worker.js:28878:18 in node_modules/methods/index.js
[code: 10021]
Uncaught TypeError: globalThis.XMLHttpRequest is not a constructor
at worker.js:28241:13 in checkTypeSupport
at worker.js:28262:40 in
C:/Users/User/AppData/Roaming/npm/node_modules/wrangler/node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/capability.js
at worker.js:16:58 in __init
at worker.js:28524:7 in
C:/Users/User/AppData/Roaming/npm/node_modules/wrangler/node_modules/rollup-plugin-node-polyfills/polyfills/http-lib/request.js
at worker.js:16:58 in __init
at worker.js:28741:7 in node-modules-polyfills:http
at worker.js:16:58 in __init
at worker.js:28860:23 in node-modules-polyfills-commonjs:http
at worker.js:19:52 in __require2
at worker.js:28878:18 in node_modules/methods/index.js
[code: 10021]
Hello, I’m Allie!
Yeah, you can only use Fetch
Mr. Goodman
Mr. GoodmanOP14mo ago
ok look here
function checkTypeSupport(type) {
if (!xhr) {
xhr = new global.XMLHttpRequest()
// If location.host is empty, e.g. if this page/worker was loaded
// from a Blob, then use example.com to avoid an error
xhr.open('GET', global.location.host ? '/' : 'https://example.com')
}
try {
xhr.responseType = type
return xhr.responseType === type
} catch (e) {
return false
}

}
function checkTypeSupport(type) {
if (!xhr) {
xhr = new global.XMLHttpRequest()
// If location.host is empty, e.g. if this page/worker was loaded
// from a Blob, then use example.com to avoid an error
xhr.open('GET', global.location.host ? '/' : 'https://example.com')
}
try {
xhr.responseType = type
return xhr.responseType === type
} catch (e) {
return false
}

}
what can i do here to make it compatible but not break it it actually only happens in this file i looked
Hello, I’m Allie!
I'm not sure you can? The rest of your code probably requires XHMLHttpRequest quirks to get it to work
Mr. Goodman
Mr. GoodmanOP14mo ago
nop only mention
Hello, I’m Allie!
Does any part of your code reference xhr?
Mr. Goodman
Mr. GoodmanOP14mo ago
no just webtorrent
Hello, I’m Allie!
Yeah, thats what I mean
Mr. Goodman
Mr. GoodmanOP14mo ago
its not my code well kinda nevermind sorry
Hello, I’m Allie!
If any code that your code relies on requires XMLHttpRequest, then it won't work
Want results from more Discord servers?
Add your server