Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

missing HTTP adapter? #456

Closed
UnsungHero97 opened this issue Sep 25, 2016 · 7 comments
Closed

missing HTTP adapter? #456

UnsungHero97 opened this issue Sep 25, 2016 · 7 comments

Comments

@UnsungHero97
Copy link

Axios v0.14.0
Node v6.6.0

I'm trying to do a simple GET, and I'm faced with ReferenceError: XMLHttpRequest is not defined. I was looking through the code in dist/axios.js, and I'm seeing something unexpected:

if (typeof config.adapter === 'function') {
  // For custom adapter support
  adapter = config.adapter;
} else if (typeof XMLHttpRequest !== 'undefined') {
  // For browsers use XHR adapter
  adapter = __webpack_require__(10);
} else if (typeof process !== 'undefined') {
  // For node use HTTP adapter
  adapter = __webpack_require__(10);
}

Webpack seems to be requiring the same dependency for both XHR and HTTP adapters, __webpack_require__(10). Further, when I search for httpAdapter and dispatchHttpRequest, nothing comes up.

Is the v0.14.0 release missing the HTTP adapter?

@UnsungHero97 UnsungHero97 changed the title getting "ReferenceError: XMLHttpRequest is not defined" missing HTTP adapter? Sep 25, 2016
@UnsungHero97
Copy link
Author

Hmmm... I changed webpack to export to node instead of web, and it's working now. Not sure what the difference is, but seems like this isn't a real problem.

@UnsungHero97
Copy link
Author

UnsungHero97 commented Oct 3, 2016

@mzabriskie ping

I just wanted to follow up on this to try and get a better understanding of why this happens. I found that the build is different depending on if webpack is targeting node or web. In the case of web, webpack is picking up the browser field from axios's package.json:

"browser": {
  "./lib/adapters/http.js": "./lib/adapters/xhr.js"
},

As such, if I configure webpack with target: 'web', I get the following in the bundle:

var adapter;
if (typeof config.adapter === 'function') {
  // For custom adapter support
  adapter = config.adapter;
} else if (typeof XMLHttpRequest !== 'undefined') {
  // For browsers use XHR adapter
  adapter = __webpack_require__("./node_modules/axios/lib/adapters/xhr.js");
} else if (typeof process !== 'undefined') {
  // For node use HTTP adapter
  adapter = __webpack_require__("./node_modules/axios/lib/adapters/xhr.js");
}

Now, when I change to target: 'node', I get the following:

var adapter;
if (typeof config.adapter === 'function') {
  // For custom adapter support
  adapter = config.adapter;
} else if (typeof XMLHttpRequest !== 'undefined') {
  // For browsers use XHR adapter
  adapter = __webpack_require__("./node_modules/axios/lib/adapters/xhr.js");
} else if (typeof process !== 'undefined') {
  // For node use HTTP adapter
  adapter = __webpack_require__("./node_modules/axios/lib/adapters/http.js");
}

Shouldn't the build be consistent no matter what webpack is targeting? Isn't the point of this if-elseif-elseif block to determine which adapter to use based on the current environment at run-time? Why even have that if-elseif-elseif block if it gets changed at build-time?

@nickuraltsev
Copy link
Member

The idea here is to not include the http adapter and its dependencies to web bundles to reduce their size.

@UnsungHero97
Copy link
Author

@nickuraltsev Got it. Any suggestions on how to build axios with both adapters included, with the goal being to bundle a single library that can run both on the web and in node?

@leepowelldev
Copy link

@UnsungHero97 Did you resolve this? I'm having the same issue - trying to build a single bundle I can use on both the browser and node. At the moment I'm having to inject the xhr adapter into the defaults in Node. It works but isn't a clean solution.

@UnsungHero97
Copy link
Author

I ended up creating 2 separate builds, each targeting their respective environments, 1 for web and 1 for node.

@Naramsim
Copy link

Naramsim commented Mar 9, 2017

Using the target: 'node' with the entry node: { process: false }, and a plugin

plugins: [
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
    })
  ]

Makes the output library both working on the web and node.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants