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

"fetch" is not defined #4015

Closed
lucasfeliciano opened this issue Oct 2, 2015 · 27 comments
Closed

"fetch" is not defined #4015

lucasfeliciano opened this issue Oct 2, 2015 · 27 comments
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion question This issue asks a question about ESLint

Comments

@lucasfeliciano
Copy link

import 'isomorphic-fetch';

expose a function called "fetch" as so react-native projects.

which rule i need to use to suppress this error just for this case?

@eslintbot
Copy link

Thanks for the issue! If you're reporting a bug, please be sure to include:

  1. The version of ESLint you are using (run eslint -v)
  2. What you did (the source code and ESLint configuration)
  3. The actual ESLint output complete with numbers
  4. What you expected to happen instead

Requesting a new rule? Please see Proposing a New Rule for instructions.

@eslintbot eslintbot added the triage An ESLint team member will look at this issue soon label Oct 2, 2015
@gyandeeps gyandeeps added question This issue asks a question about ESLint and removed triage An ESLint team member will look at this issue soon labels Oct 2, 2015
@ilyavolodin
Copy link
Member

As this is react-specific feature, you might want to create an issue in eslint-plugin-react repository. Or they might already support this (not sure). ESLint doesn't support react out of the box.

@lucasfeliciano
Copy link
Author

I tried to use the plugin, but this is a feature from react-native so it is not implemented.

Besides that other modules could export that function, like import 'isomorphic-fetch'; does

@btmills
Copy link
Member

btmills commented Oct 2, 2015

If you just want to make sure ESLint knows about the global variable, check out the Specifying Globals section of our configuring docs. Will /*global fetch:false*/ work?

@lucasfeliciano
Copy link
Author

But I need to add this comment in every file that I use the fetch(), right?

It would be better to add a rule in .eslintrc

@btmills
Copy link
Member

btmills commented Oct 2, 2015

Skip down a line in the docs, how about "globals": { "fetch": false }?

@ilyavolodin
Copy link
Member

@lucasfeliciano Does @btmills suggestion helped?

@lucasfeliciano
Copy link
Author

Hey guys, I'm traveling in brazil right now and didn't have a chance to
test.
I'll do this when I have a chance to close the ticket.
:)
Thanks for the help until now

On Sun, 4 Oct 2015 at 22:11, Ilya Volodin notifications@github.com wrote:

@lucasfeliciano https://github.com/lucasfeliciano Does @btmills
https://github.com/btmills suggestion helped?


Reply to this email directly or view it on GitHub
#4015 (comment).

@nzakas
Copy link
Member

nzakas commented Oct 7, 2015

OK, closing this issue as there doesn't appear to be any task here.

@nzakas nzakas closed this as completed Oct 7, 2015
@SimenB
Copy link
Contributor

SimenB commented Oct 22, 2015

fetch is a global though, isn't it? https://developer.mozilla.org/en/docs/Web/API/Fetch_API

isomorphic-fetch is just a polyfill, IIRC. Whether fetch should be a predefined global I don't know, but it's not a react specific feature at all, unless I've misunderstood something

@sarkistlt
Copy link

sarkistlt commented Jun 24, 2016

same issue, in comment it works in config doesn't, what the problem?

@ghost
Copy link

ghost commented Sep 9, 2016

Set the environment for a browser inside the .eslintrc file:

env:
  browser: true

@divyanshu013
Copy link

Just adding to @julius-mfc, the following will work in a non-yaml fashion:

"env": {
    "browser": true
  }

@ajostergaard
Copy link

ajostergaard commented May 16, 2017

Any response to @SimenB 's comment?

@SimenB
Copy link
Contributor

SimenB commented May 16, 2017

Wow, 2015.

The Ghost and @divyanshu013 are correct though, use browser env, then fetch is defined 😄

@chee
Copy link

chee commented May 16, 2017

it might be preferable to use

/* global fetch:false */

at the top of your file

or

"globals": {
  "fetch": false
}

in your config, so as not to pull in other globals from the browser env.

(the false stops you from being allowed to overwrite it)

@ajostergaard
Copy link

Thanks @SimenB but the question you asked wasn't how to get around the issue, the question was why we have to get around it at all given that fetch is a global - no?!?

@SimenB
Copy link
Contributor

SimenB commented May 17, 2017

fetch is only a global in browsers, not in Node. Globals without specifying anything else is (mostly) just language globals, not api globals. It's the same reason you have to specify node in env to use require, but not import

@ajostergaard
Copy link

Ahh - thanks! :) So really the react plugin should handle this - shouldn't it?

@SimenB
Copy link
Contributor

SimenB commented May 17, 2017

No, not unless React automatically polyfills fetch in every environment (which it doesn't). Either tell eslint your code runs in a browser, or explicitly tell it fetch is a global.

@ajostergaard
Copy link

So really React Native, which does provide fetch, should also provide a plugin or at least document the 'integration'. :)

@platinumazure
Copy link
Member

I think this would be an excellent discussion to have in the eslint-plugin-react and/or react-native repositories. 😉

@maulikdhameliya
Copy link

maulikdhameliya commented Oct 7, 2017

add below lines in .eslintrc
"env": {
"browser": true
},
"globals": {
"fetch": true
}

and then edit something in file and save. Your error about "fetch" is not defined will be removed

@chee
Copy link

chee commented Oct 7, 2017

@maulikdhameliya you'll probably want "fetch": false
the boolean is about whether or not the global variable should be assignable in your code

as in

// without fetch in globals
fetch = 'lol' // fetch is not defined

// with fetch: true
fetch = 'lol' // fine

// with fetch: false
fetch = 'lol' // error (if using `no-global-assign` (enabled in eslint:recommended))

davidbasalla added a commit to redbadger/pride-london-app that referenced this issue Jan 12, 2018
Contentful provide a client via the contentful npm package and they
provide some boilerplate code. This commit aims to mirror the client's
interface (only one method at the moment, for Pride events) and return
data from the mock server.

https://github.com/contentful/contentful.js

Add `browser: true` to eslintrc file, otherwise it complains that the `fetch`
method in MockClient has not been defined.
See eslint/eslint#4015
davidbasalla added a commit to redbadger/pride-london-app that referenced this issue Jan 12, 2018
Contentful provide a client via the contentful npm package and they
provide some boilerplate code. This commit aims to mirror the client's
interface (only one method at the moment, for Pride events) and return
data from the mock server.

https://github.com/contentful/contentful.js

Add `browser: true` to eslintrc file, otherwise it complains that the `fetch`
method in MockClient has not been defined.
See eslint/eslint#4015
@leoskyrocker
Copy link

leoskyrocker commented Jan 22, 2018

From what I'm reading, react-native developers should prefer using

"globals": {"fetch": false}

over

"env": {"browser": true}

because apparently react-native is not 100% equivalent to browser env.

One example is the atob function which is not in react-native's global, but is in the list of browser globals.
That means by specifying the browser env, we will also pull in variables that are not available in RN.

@SimenB
Copy link
Contributor

SimenB commented Jan 22, 2018

For RN you could also go for something like https://github.com/satya164/eslint-plugin-react-native-globals or potentially open up at issue at https://github.com/sindresorhus/globals so you could do {"env": "react-native": true}.

@j-f1
Copy link
Contributor

j-f1 commented Jan 22, 2018

or potentially open up at issue at https://github.com/sindresorhus/globals so you could do {"env": "react-native": true}.

sindresorhus/globals#82

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 7, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion question This issue asks a question about ESLint
Projects
None yet
Development

No branches or pull requests