Wasm-bindgen fetch example doesn't work on NodeJS

The example for JavaScript's fetch API works perfectly when compiling the WASM module for the browser.
However, as soon as I try to build it for NodeJS, the build itself succeeds, the module loads properly, but when I try to call the run function, I get this error:

> var module = require('./pkg')
undefined
> module.run("rustwasm/wasm-bindgen").then(m => console.log(m)).catch(e => console.error(e))
Promise {
  <pending>,
  domain:
   Domain {
     domain: null,
     _events:
      [Object: null prototype] {
        removeListener: [Function: updateExceptionCapture],
        newListener: [Function: updateExceptionCapture],
        error: [Function: debugDomainError] },
     _eventsCount: 3,
     _maxListeners: undefined,
     members: [],
     [Symbol(kWeak)]: WeakReference {} } }
> ReferenceError: Request is not defined
    at /home/j/dev/accept/wasm-bindgen/examples/fetch/pkg/fetch.js:260:15
    at /home/j/dev/accept/wasm-bindgen/examples/fetch/pkg/fetch.js:185:22
    at wasm-function[181]:21
    at wasm-function[42]:325
    at wasm-function[114]:82
    at wasm-function[102]:115
    at wasm-function[219]:14
    at __wbg_adapter_20 (/home/j/dev/accept/wasm-bindgen/examples/fetch/pkg/fetch.js:168:10)
    at real (/home/j/dev/accept/wasm-bindgen/examples/fetch/pkg/fetch.js:153:20)
    at process._tickCallback (internal/process/next_tick.js:68:7)

So my core question is: how do I access the fetch API from Rust in a way that's compatible with both the browser and NodeJS? It's important that those 2 use the same API: to do otherwise would just needlessly complicate matters, both for me and for the consumer of the code.

By using the fetch implementation for NodeJS, such as node-fetch. NodeJS simply doesn't have it out-of-the-box.

Yeah in the meantime I figured that out too.
I'm thinking of open-sourcing the FFI crate, because the fact that this is necessary in order to use fetch from both the browser and NodeJS in a consistent way is... less than optimal.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.