Target node environment when building with wrangler

I'm trying to use @mapbox/mapbox-sdk in my worker, however they have a browser entry point defined in their package.json which relies on window.XMLHttpRequest object being present, which causes the following error
[wrangler:err] ReferenceError: window is not defined
at createRequestXhr (file:///<snip>/worker/node_modules/@mapbox/mapbox-sdk/lib/browser/browser-layer.js:105:13)
at null.<anonymous> (file:///<snip>/worker/node_modules/@mapbox/mapbox-sdk/lib/browser/browser-layer.js:115:15)
[wrangler:err] ReferenceError: window is not defined
at createRequestXhr (file:///<snip>/worker/node_modules/@mapbox/mapbox-sdk/lib/browser/browser-layer.js:105:13)
at null.<anonymous> (file:///<snip>/worker/node_modules/@mapbox/mapbox-sdk/lib/browser/browser-layer.js:115:15)
Is it possible to either polyfill window.XMLHttpRequest, or target a node environment when bundling via wrangler?
1 Reply
Almamun
Almamun11mo ago
Not sure the mapbox prodive any server-side sdk. So this version is not compatible with cloudeflare workers. Nodejs and Cloudeflare Workers architecture is quit different that's why many npm dependency doesn't work on cloudeflare workers. So my suggestion to use alternative of mapbox or you can reach out to mapbox support if they maintain any discord channel or through github.
Want results from more Discord servers?
Add your server