Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign up"fetch" is not defined #821
Comments
This comment has been minimized.
This comment has been minimized.
|
Use window.fetch (or document.fetch, forgot which one it is). The browser
has too many globals to whitelist, so referencing the object it's attached
to is a good thing
…On Wed, Mar 15, 2017, 20:19 Yao Ding ***@***.***> wrote:
When I use fetch, a polyfill is imported like this import 'whatwg-fetch',
and "fetch" is not defined error would show up.
I know that I could use "global" to prevent this error, but should these
been considered in standard for these common polyfill?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#821>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACWleif2D2GTbNsUMFFSsF9bqIYhSOjqks5rmDnPgaJpZM4MeaH3>
.
|
This comment has been minimized.
This comment has been minimized.
|
@yoshuawuyts Thanks! I should have thought of using window before. |
yaodingyd
closed this
Mar 15, 2017
This comment has been minimized.
This comment has been minimized.
|
Yay, glad I could be of help!
…On Wed, Mar 15, 2017 at 8:35 PM Yao Ding ***@***.***> wrote:
Closed #821 <#821>.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#821 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ACWlemyQn1-ETrxbQPXMvqcmcsIedZNtks5rmD2YgaJpZM4MeaH3>
.
|
This comment has been minimized.
This comment has been minimized.
indiesquidge
commented
Mar 18, 2017
•
|
@yoshuawuyts, how does this take into account server-side use of |
This comment has been minimized.
This comment has been minimized.
|
It may be just be a import from a package like node-fetch? it would be inside your file scope as import so no need for global. |
This comment has been minimized.
This comment has been minimized.
|
Exactly what @falmar said. In node, you just do: const fetch = require('node-fetch')
fetch('https://github.com/', ...) |
This comment has been minimized.
This comment has been minimized.
indiesquidge
commented
Mar 18, 2017
•
|
Hmm, aren't things like |
This comment has been minimized.
This comment has been minimized.
|
Not a good idea, IMO. As a rule, modules shouldn't set global variables. A better approach would be to import fetch in both node and the browser environment. (In the browser, it could simply return If you still want to use import "isometric-fetch" /* global fetch */ |
This comment has been minimized.
This comment has been minimized.
indiesquidge
commented
Mar 18, 2017
•
|
Not a good idea as in "I don't recommend using I thought |
This comment has been minimized.
This comment has been minimized.
indiesquidge
commented
Mar 18, 2017
I'm currently doing it this way, but it did feel kind of icky, and I was hoping there was a better solution. However, I'm not sure I fully understand what you're suggesting as an alternative. Thanks for replying so quickly to all of this though! |
This comment has been minimized.
This comment has been minimized.
|
const fetch = require('isomorphic-fetch')
fetch('http://example.com')
.then(res => res.text())
.then(text => console.log(text)) |
This comment has been minimized.
This comment has been minimized.
indiesquidge
commented
Mar 19, 2017
•
This comment has been minimized.
This comment has been minimized.
indiesquidge
commented
Mar 19, 2017
|
Apologies, I mis-read your comment and see now that you say "in addition to patching the global scope". However, if |
This comment has been minimized.
This comment has been minimized.
|
I highly recommend you to use it like @LinusU in his snippet. It is way more declarative and you will know exactly what is going on. I do expect that |
This comment has been minimized.
This comment has been minimized.
indiesquidge
commented
Mar 19, 2017
How? The library is replacing the global
I'm not sure I fully understand what you're getting at here, but I think you may be misunderstanding |
This comment has been minimized.
This comment has been minimized.
I think the point is that the global |
This comment has been minimized.
This comment has been minimized.
indiesquidge
commented
Mar 19, 2017
I'm pretty confused by this. Wouldn't the global fetch just be set by |
This comment has been minimized.
This comment has been minimized.
In an ideal world, no one would include multiple polyfills in a single project. However, if one of the packages you're using includes It's not that complicated: packages should never write globals. If you, as a top-level user who is making an app, want to polyfill if (typeof window.fetch !== 'function') {
window.fetch = require('some-fetch-implementation')
}That said,
const fetch = require('isomorphic-fetch')
fetch('http://example.com')
require('isomorphic-fetch') /* global fetch */
fetch('http://example.com')This is all documented in the readme here: http://standardjs.com/#i-use-a-library-that-pollutes-the-global-namespace-how-do-i-prevent-variable-is-not-defined-errors |
This comment has been minimized.
This comment has been minimized.
indiesquidge
commented
Mar 19, 2017
•
|
Ah, that makes perfect sense, @feross. I really appreciate you taking the time to explain this, as I know you likely have many other things competing for your attention. I agree 100% with what you have laid out now that is has been explained in more detail. (Apologies for somehow missing that section in the README.) Thanks again. |
This comment has been minimized.
This comment has been minimized.
|
No problem! |
This comment has been minimized.
This comment has been minimized.
karnavkumar
commented
Mar 21, 2018
|
use this |
This comment has been minimized.
This comment has been minimized.
kranthi1996
commented
Apr 10, 2018
•
|
npm install --save isomorphic-fetch es6-promise const fetch = require('isomorphic-fetch') its working fine |
yaodingyd commentedMar 15, 2017
When I use
fetch, a polyfill is imported like thisimport 'whatwg-fetch', and"fetch" is not definederror would show up.I know that I could use "global" to prevent this error, but should these been considered in standard for these common polyfill?