fix: import xhr2 to avoid reference error (#101)#102
fix: import xhr2 to avoid reference error (#101)#102tkt9k2562 wants to merge 1 commit intodcmjs-org:masterfrom
Conversation
|
I test it manually , by replacing the |
|
What does this do on a browser? Is it transparent and workable in node and browser or do we need to detect? @wayfarer3130 @sedghi do you have any thoughts on this PR? |
|
I don't know that it is going to work in a browser - that is something which would have to be tested to see what it does. One could export a method that just choose that import versus the browser version depending on if in node or the browser. |
|
Thanks for the doublecheck @wayfarer3130 . @tkt9k2562 can you look into making sure this works the same in node and the browser? Ideally with automated testing. |
|
I don't think this is the way, not sure what we need from xhr request but we can do something simpler like let fetch;
if (typeof window === 'undefined') {
fetch = await import('node-fetch').then(mod => mod.default);
} else {
fetch = window.fetch; // Use browser's native fetch
}or the solution above but dynamically choosing/importing that package instead of forcing it at import level |
|
I'd suggest closing this issue no fix and just using one of the more complete nodejs browser emulators such as the one that jest uses. That way it works in a nodejs and in a browser environment. Also, I think nodejs 24 now has the XMLHttpRequest natively. |
Original Issue:
#101