mattmccabe
05/02/2023, 10:36 PMconst url_1 = require("url");
...
...
...
const url = new url_1.URL(input);//throws undefined
When I run wrangler dev I receive an undefined error I am guessing because the worker env doesn't know how to resolve the require("url") which is built into the nodejs runtime environment.
Is there a way to bundle some kind of replacement for nodejs's url lib/module in order to get the require("url") to resolve correctly inside of the worker runtime.
I was looking at the Configuration > Bundling documentation in the Wrangler docs but no lightbulbs have gone off on how to leverage it to provide a solution.
Any experience anyone can share would be greatly appreciated!kian
05/02/2023, 10:37 PMURL
global?kian
05/02/2023, 10:37 PMkian
05/02/2023, 10:37 PMkian
05/02/2023, 10:38 PMmattmccabe
05/02/2023, 10:38 PMmattmccabe
05/02/2023, 10:39 PMmattmccabe
05/02/2023, 10:39 PM//const url_1 = require("url");
...
...
...
const url = new URL(input);//it works!
mattmccabe
05/02/2023, 10:42 PMkian
05/02/2023, 10:42 PMesbuild
is what bundles your NPM dependencies.kian
05/02/2023, 10:43 PMnode_compat = true
to your Wrangler.toml which adds some polyfills but you can't selectively just include the url
module so it adds some bloat.mattmccabe
05/02/2023, 10:48 PMnode_compat = true
as well as the compatibility_flags = [ "nodejs_compat" ]
mattmccabe
05/02/2023, 11:30 PMwrangler publish --dry-run --outdir dist
and do see that require("url"); being replaced with require_url() which is defined as this:
// node-modules-polyfills-commonjs:url
var require_url = __commonJS({
"node-modules-polyfills-commonjs:url"(exports2, module2) {
init_virtual_process_polyfill();
init_buffer();
var polyfill = (init_url(), __toCommonJS(url_exports));
if (polyfill && polyfill.default) {
module2.exports = polyfill.default;
for (let k in polyfill) {
module2.exports[k] = polyfill[k];
}
} else if (polyfill) {
module2.exports = polyfill;
}
}
});
So maybe its an issue where the polyfill isn't working in this scenariomattmccabe
05/02/2023, 11:40 PM