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

self is not defined #125

Closed
tcurdt opened this issue Apr 18, 2015 · 10 comments
Closed

self is not defined #125

tcurdt opened this issue Apr 18, 2015 · 10 comments

Comments

@tcurdt
Copy link

tcurdt commented Apr 18, 2015

I have an isomorphic react app. So while this the code is not running on node, still babel complains that "self is not defined". A quick hack that makes things work is to add "self = this;" to fetch.

Would be really nice to have a proper fix for this.

@tcurdt
Copy link
Author

tcurdt commented Apr 18, 2015

just found https://github.com/matthew-andrews/isomorphic-fetch and then https://github.com/matthew-andrews/fetch/blob/master/fetch.js#L3 which has:

if (typeof global !== 'undefined') {
  var self = global.self;
}

@mislav
Copy link
Contributor

mislav commented Apr 19, 2015

Yes please use that project instead. #31

@mislav mislav closed this as completed Apr 19, 2015
@tcurdt
Copy link
Author

tcurdt commented Apr 19, 2015

I don't want to really use it in isomorphic way - I just have an isomorphic setup. That's a bit of a different thing. (Which is why those projects are not really what I am after)

Ideally require("whatwg-fetch") would just export what it's offering.
What I ended up doing is:

if (process.env.IS_BROWSER) {
  require('whatwg-fetch');
}

Not nice - but works.

@mislav
Copy link
Contributor

mislav commented Apr 20, 2015

Your workaround looks good. I would suggest applying the same conditional to all requires in your app that load libraries that are only useful in browsers.

@tcurdt
Copy link
Author

tcurdt commented Apr 20, 2015

I will.

As a side note - I would much prefer this style of use

var blafetch = require('whatwg-fetch');
blafetch('/users.html')
  .then(function(response) {
    return response.text()
  }).then(function(body) {
    document.body.innerHTML = body
  })

or even better

var Fetch = require('whatwg-fetch');
Fetch.get('/users.html')
  .then(function(response) {
    return response.text()
  }).then(function(body) {
    document.body.innerHTML = body
  })

Maybe for the next major version.

@mislav
Copy link
Contributor

mislav commented Apr 20, 2015

As a side note - I would much prefer this style of use

Perhaps you're missing the point of this project. We're trying to create a polyfill for a future standard feature of browsers. The API for that feature is window.fetch. We're not trying to create anything more or other than that.

@tcurdt
Copy link
Author

tcurdt commented Apr 20, 2015

I see.

@billinghamj
Copy link
Contributor

Unfortunately in React Native, the workarounds mentioned don't seem to be sufficient. Due to the way React packages JS files assuming they support AMD/CommonJS modularization properly, this library ends up running anyway - even if it isn't required.

@mislav
Copy link
Contributor

mislav commented Jan 5, 2016

@billinghamj We welcome a PR that solves this while keeping in mind that the code must also be able to run in a web worker environment. See #239 (comment) for context

@billinghamj
Copy link
Contributor

@mislav That was the purpose of #252. I'll make a PR for it.

eliperkins added a commit to useshortcut/shortcut-client-js that referenced this issue May 25, 2017
This was causing issues on projects targeting React Native, as the isomorphic-fetch polyfill did not support the correct lookup in the packager when determining what `fetch` module to use.

See JakeChampion/fetch#125 (comment), matthew-andrews/isomorphic-fetch#63
nitrog7 added a commit to nitrogenlabs/DefinitelyTyped that referenced this issue Aug 28, 2017
React Native runs into an issue where self is undefined when using the global fetch method. (See JakeChampion/fetch#125). This typing definition allows the fix to be made.
Empact added a commit to Empact/react-native that referenced this issue Mar 18, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 7, 2020
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

3 participants