Search code examples
javascriptsuperagentparceljs

.on is not a function error when trying to upload file with superagent on parcel


Having this set up using superagent

this.acceptContentType = "application/json";

req = Request.post(absoluteUrl)
 .timeout(config.runtime.apiTimeoutMs)
 .set('Accept', this.acceptContentType);

req.attach('files', file, file.name);

When running this, im getting an error stating .on is not a function. Did some research, and it seems to be related to the FormData() object. For some reason superagent exposes node-internals to my client. This worked fine using Webpack, but after porting to Parcel, this is broken. Any pointers would be appreciated.


Solution

  • According to the documentation for Superagent, Webpack and Browserify "knows" to fetch the client-version. Parcel picked the Node-implementation. To force this, i changed:

    import Request from 'superagent';
    

    to

    import Request from 'superagent/lib/client';
    

    That seems to fix the issue.