lucasfeliciano / fetch-everywhere

WHATWG Fetch API, for ALL JavaScript environments!
MIT License
52 stars 13 forks source link

Seems to pack with webpack just fine but I get this error: #1

Closed dalanmiller closed 8 years ago

dalanmiller commented 8 years ago
...../node_modules/fetch-everywhere/fetch-npm-browserify.js:7
module.exports = globalObj.fetch.bind(globalObj);
                                ^
TypeError: Cannot read property 'bind' of undefined

Why would this be happening? fetch doesn't seem to be available.

flipace commented 8 years ago

this happens because whatwg-fetch didn't add .fetch to "globalObj" in your case. it occurs if globalObj is not the chosen target for whatwg-fetch.

When using webpack, you'll have to import/export isomorphic-fetch as follows:

global.self = global
require('imports?this=>global!exports?global.fetch!isomorphic-fetch')
lucasfeliciano commented 8 years ago

Hey @dalanmiller, thank you for the issue.

I'll investigate it and comeback with a PR or something else to add to the discussion.

Can you tell me your env versions?

lucasfeliciano commented 8 years ago

I just realised that the read me was wrong.

you should npm install fetch-everywhere

This error that you getting is basically the motivation for the fork.

Can you try install the right module and see if it works?

dpickett commented 8 years ago

I installed fetch-everywhere per the updated README but I still receive the same error message.

willmcclellan commented 8 years ago

I had the same problem. It seems as though this is not referring to the global object in fetch-npm-browserify, but you can get at it with self

// the whatwg-fetch polyfill installs the fetch() function
// on the global object (window or self)
//
// Return that as the export for use in Webpack, Browserify etc.
require('whatwg-fetch');
var globalObj = self || this.self || this;
module.exports = globalObj.fetch.bind(globalObj);
lucasfeliciano commented 8 years ago

True that, sorry for the one month delay on this issue.

Startup life was consuming me.

@willmcclellan is right. Making the fix now.

SYU15 commented 8 years ago

Hi,

I see this issue is closed, but I am still receiving this error when using it with webpack. Is there a workaround I can use to to fix the issue? It seems like my code isn't using "self", "this.self" or "this" for some reason. When I tried using:

global.self = global require('imports?this=>global!exports?global.fetch!isomorphic-fetch')

I got an error saying "Cannot resolve module 'imports'" when I tried to build it via webpack.

Thanks!

alvaromb commented 8 years ago

I'm experiencing this in a React Native environment. Please let me know if I can help you with anything to solve this issue.

alvaromb commented 8 years ago

In my case, it seems the module is interfering with React Native's InteractionManager promise. I get the Cannot read property 'bind' of undefined.