React Native Error - 无法找到变量:self

Nir*_*ngi 5 node.js react-native isomorphic-fetch-api

isomorphic-fetch在React Native应用程序中添加节点模块(版本:^ 2.2.1)后,我收到错误Can't find variable: self.
这里我附上了截图.

在此输入图像描述

从位于node_modules> isomorphic-fetch>的文件中抛出错误fetch-npm-browserify.js.
以下是该文件的代码.

// 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);
Run Code Online (Sandbox Code Playgroud)

小智 6

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

谷歌翻译:@Nirav Dangi,按照我的项目中的新方式fetch-npm-react-native.js无法正常工作; 我在这里处理直接更改,node_modules> isomorphic-fetch> fetch-npm-browserify.js文件返回:

var globalObject = typeof self === "undefined" ? global : self;
module.exports = globalObject.fetch.bind(globalObject);
//module.exports = fetch;
Run Code Online (Sandbox Code Playgroud)


Nir*_*ngi 2

我在这个答案的帮助下解决了这个问题,
https://github.com/matthew-andrews/isomorphic-fetch/pull/80

为 React Native 指定一个单独的入口点,导出 React Native 的 fetch() polyfill。


  1. 创建一个名为 的空文件fetch-npm-react-native.js。将此文件拖放到该node_modules > isomorphic-fetch位置。
  2. 将此行代码添加module.exports = fetch;到 fetch-npm-react-native.js 文件中。
  3. 转到文件node_modules > isomorphic-fetch > package.json。在package.json文件中,添加"main": "fetch-npm-node.js",代码行。
  4. 打开终端,转到应用程序目录并写入npm install
  5. 重新加载应用程序,然后就可以了...


当然这要归功于Jou