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
kian8mo ago
ES modules are the recommended format for Workers What errors did you get?
Mr. Goodman
Mr. Goodman8mo 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
kian8mo ago
Those are Node modules, which aren't available on Workers.
Mr. Goodman
Mr. Goodman8mo 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. Goodman8mo 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. Goodman8mo 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. Goodman8mo 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. Goodman8mo ago
But I got some global xmlhttprequest error
Hello, I’m Allie!
Yeah, Workers only have Fetch
Mr. Goodman
Mr. Goodman8mo 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. Goodman8mo 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. Goodman8mo 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. Goodman8mo ago
nop only mention
Hello, I’m Allie!
Does any part of your code reference xhr?
Mr. Goodman
Mr. Goodman8mo ago
no just webtorrent
Hello, I’m Allie!
Yeah, thats what I mean
Mr. Goodman
Mr. Goodman8mo 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
More Posts
cdnI'm trying to understand a bit about traffic costs. I'm planning to serve a big download, probably aCloudflare Page fails to build while importing a plugin that delivers static assetsCloudflare Deployment Id: 685318ba-e9d9-4b34-a246-3ccbd5b61e1d The build pipeline I have setup therAnti-DDoS for Game Server Hosting with CloudflareHello, I have a question regarding anti-DDoS protection for my game server. I'm curious if CloudflaAFAIK `unstable_dev` is local-only, soAFAIK `unstable_dev` is local-only, so it might be failing because Hyperdrive doesn't work locally?Setting up a static website generator with Cloudflare PagesHi, We are dev team working on a new feature on top of our platform for restaurant reservations manunable to connect to different subdomains with srv recordsI'm trying to set up different subdomains in my DNS that all point to different ports on the same IPworkers limitation and server-side-rendering, data storage and sveltekit. Help me understand pleaseHello I have been researching on workers and still haven figured out, if i can use it for a fullstaInfrequent SSL and reset connection errors serving images through a worker.Hi! I’m using a paid worker to return images through a worker attached to a custom domain (picperf.dCould CF / R2 interfere with legit traffic?i know for a fact i will have a big spike in downloads roughly a week from now. i don't get too muchSince this afternoon Error when deploying all our nuxt applicationsApp: xavi-blog-nuxt-wp.pages.dev 20:00:23.177 Deploying your site to Cloudflare's global network