5

After adding a node module,isomorphic-fetch(version : ^2.2.1) in React Native app, I am getting an error of Can't find variable: self.
Here I have attached a screenshot.

enter image description here

The error is thrown from the file, located at node_modules > isomorphic-fetch > fetch-npm-browserify.js.
Following is the code of that file.

// 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');
module.exports = self.fetch.bind(self);
Nirav Dangi
  • 3,607
  • 4
  • 49
  • 60

3 Answers3

6

@Nirav Dangi,按新建fetch-npm-react-native.js 的方式在我的项目里面没有起作用; 我这边处理方式,是直接修改,node_modules > isomorphic-fetch > fetch-npm-browserify.js 文件的返回:

Google Translate: @Nirav Dangi, in accordance with the new way fetch-npm-react-native.js in my project which is not working; I am here to deal with direct changes, node_modules> isomorphic-fetch> fetch-npm-browserify.js file Back:

var globalObject = typeof self === "undefined" ? global : self;
module.exports = globalObject.fetch.bind(globalObject);
//module.exports = fetch;
Anil
  • 21,730
  • 9
  • 73
  • 100
galaxybing
  • 61
  • 1
  • I am not good enough with the Chinese language. So far I understood by your code, it should work. Also, If you can see in my answer, I have just updated it and added #4 `npm install`. – Nirav Dangi Jun 20 '16 at 15:10
2

I have fixed this issue with the help of this answer,
https://github.com/matthew-andrews/isomorphic-fetch/pull/80

Specify a separate entry point for React Native that export React Native's fetch() polyfill.


  1. Create an empty file named fetch-npm-react-native.js. Drop this file at node_modules > isomorphic-fetch location.
  2. Add this line of code module.exports = fetch; to fetch-npm-react-native.js file.
  3. Goto the file node_modules > isomorphic-fetch > package.json. In the package.json file, add "main": "fetch-npm-node.js", line of code.
  4. Open Terminal, GoTo app directory and write npm install
  5. Reload the app and here you go...


Ofcourse the credit goes to Jou

Nirav Dangi
  • 3,607
  • 4
  • 49
  • 60
0

I had the same problem, and the other solutions didn't work for me.

What I did was to replace

import 'isomorphic-fetch'

with this:

import 'cross-fetch'

This is based on the following: https://github.com/matthew-andrews/isomorphic-fetch/issues/125

Yossi
  • 5,577
  • 7
  • 41
  • 76