简体   繁体   English

导入react-native-fetch-blob时出现意外令牌

[英]Unexpected Token when importing react-native-fetch-blob

We are trying to import the react-native-fetch-blob package package using the following code: 我们正在尝试使用以下代码导入react-native-fetch-blob软件包:

const RNFetchBlob = require('react-native-fetch-blob');
const firebase = require('firebase');

However, when we try and build, we get a Syntax Error for Unexpected token import as follows. 但是,当我们尝试构建时,我们会收到意外令牌导入的语法错误,如下所示。

C:\\Users\\ ...\\node_modules\\react-native-fetch-blob\\index.js:5 C:\\ Users \\ ... \\ node_modules \\ react-native-fetch-blob \\ index.js:5
import { import {
^^^^^^ ^^^^^^
SyntaxError: Unexpected token import SyntaxError:意外的令牌导入
at Object.exports.runInThisContext (vm.js:76:16) at Object.exports.runInThisContext(vm.js:76:16)
at Module._compile (module.js:513:28) 在Module._compile(module.js:513:28)
at Object.Module._extensions..js (module.js:550:10) at Object.Module._extensions..js(module.js:550:10)
at Module.load (module.js:458:32) 在Module.load(module.js:458:32)
at tryModuleLoad (module.js:417:12) 在tryModuleLoad(module.js:417:12)
at Function.Module._load (module.js:409:3) 在Function.Module._load(module.js:409:3)
at Module.require (module.js:468:17) 在Module.require(module.js:468:17)
at require (internal/module.js:20:19) at require(internal / module.js:20:19)
at Object. 在对象。 (C:\\Users\\ ...\\build\\cloud\\file.js:159:243) at Module._compile (module.js:541:32) (C:\\ Users \\ ... \\ build \\ cloud \\ file.js:159:243)在Module._compile(module.js:541:32)

We are using ES6 and our .babelrc file looks as follows 我们使用的是ES6,我们的.babelrc文件如下所示

"plugins": [
    ["transform-runtime", {
        "polyfill": false,
        "regenerator": true
    }]
],
"presets": ["react-native","es2015",]

Is there a solution to this? 这个问题有方法解决吗? Any help would be greatly appreciated! 任何帮助将不胜感激!

Thanks! 谢谢!

I was able to work around this problem by creating a mock for react-native-fetch-blob (rn-fetch-blob, in my case). 我能够通过为react-native-fetch-blob(在我的例子中为rn-fetch-blob)创建一个模拟来解决这个问题。 The mock suggested here worked for me: https://github.com/wkh237/react-native-fetch-blob/issues/212#issuecomment-340706825 这里建议的模拟对我有用https//github.com/wkh237/react-native-fetch-blob/issues/212#issuecomment-340706825

As @scerelli I also wanted the mock to happen in a separate place so it can be used by all tests. 作为@scerelli我也希望模拟在一个单独的地方发生,所以它可以被所有测试使用。 Placing the following code in / mocks /react-native-fetch-blob.js seems to work for me: 将以下代码放在/ mocks /react-native-fetch-blob.js中似乎对我有用:

const existsMock = jest.fn(); const existsMock = jest.fn(); existsMock.mockReturnValueOnce({then: jest.fn()}); existsMock.mockReturnValueOnce({then:jest.fn()});

export default {
DocumentDir: () => {},
ImageCache: {
    get: {
        clear: () => {},
    },
},
fs: {
    exists: existsMock,
    dirs: {
        MainBundleDir: () => {},
        CacheDir: () => {},
        DocumentDir: () => {},
    },
    },
};

It's a variation of @SoundBlaster solution, just in a global mock file. 它是@SoundBlaster解决方案的变体,仅在全局模拟文件中。 With that I don't need to import or implicitly mock anything in the test code. 有了它,我不需要导入或隐式模拟测试代码中的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 react-native-fetch-blob链接失败 - react-native-fetch-blob link FAILED 如何使用 react-native-fetch-blob 创建目录和路径 - How to use react-native-fetch-blob for making directories and paths React-Native:需要帮助使用React-Native-Fetch-Blob访问Android捆绑图像 - React-Native : Need Help Accessing Android Bundled Images with React-Native-Fetch-Blob 无法读取未定义 React-Native Firebase React-native-fetch-blob 的属性“DocumentDir” - Cannot read property 'DocumentDir' of undefined React-Native Firebase React-native-fetch-blob 如何使用react-native-fetch-blob将文件从app Document文件夹移动到Iphone的文档文件夹 - How to move file from app Document folder to Iphone's document folder using react-native-fetch-blob 反应本机语法错误意外的令牌获取api - react native syntax error unexpected token fetch api React SSR:SyntaxError:导入图像时令牌无效或意外 - React SSR: SyntaxError: Invalid or unexpected token when importing image 从第三方库导入 React 项目时出现意外的标记“&lt;” - Unexpected token “<” when importing from third party library into React project Chai 导入时出现意外令牌 - Chai Unexpected token when importing 导入模块时出现意外令牌 { - Unexpected token { when importing a module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM