【问题标题】:Unexpected Token when importing react-native-fetch-blob导入 react-native-fetch-blob 时出现意外的令牌
【发布时间】:2017-06-22 04:32:39
【问题描述】:

我们正在尝试使用以下代码导入 react-native-fetch-blob 包:

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

但是,当我们尝试构建时,我们会收到以下意外令牌导入的语法错误。

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

我们使用的是 ES6,我们的 .babelrc 文件如下所示

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

有解决办法吗?任何帮助将不胜感激!

谢谢!

【问题讨论】:

  • 您找到解决问题的方法了吗?我的测试抛出此错误时遇到了类似的问题。
  • 想知道和@DavidNathan一样好运吗?

标签: javascript node.js react-native ecmascript-6 babeljs


【解决方案1】:

我能够通过为 react-native-fetch-blob(在我的例子中为 rn-fetch-blob)创建一个模拟来解决这个问题。这里建议的模拟对我有用:https://github.com/wkh237/react-native-fetch-blob/issues/212#issuecomment-340706825

作为@scerelli,我还希望模拟发生在一个单独的地方,以便所有测试都可以使用它。将以下代码放入 /mocks/react-native-fetch-blob.js 似乎对我有用:

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

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

这是@SoundBlaster 解决方案的一个变体,只是在一个全局模拟文件中。这样我就不需要在测试代码中导入或隐式模拟任何内容。

【讨论】:

  • 我必须创建文件夹 root/__mocks__/rn-fetch-blob.js
猜你喜欢
  • 1970-01-01
  • 2020-12-30
  • 1970-01-01
  • 2019-05-29
  • 2017-03-24
  • 2017-05-24
  • 2020-05-11
  • 2018-11-24
  • 2018-01-21
相关资源
最近更新 更多