未捕获的参考错误:React本机0.59.5中未定义require

时间:2019-04-23 11:02:06

标签: javascript react-native

升级到React Native 0.59.5后,我得到:

Uncaught reference error: require is not defined

babel.config.js

module.exports = {
  presets: [
    'module:metro-react-native-babel-preset',
    '@babel/preset-flow',
    [
      '@babel/preset-env',
      {
        targets: {
          esmodules: true,
        },
      },
    ],
    'airbnb',
  ],
  plugins: [
    'transform-class-properties',
    ['@babel/plugin-proposal-decorators', { legacy: true }]
  ],
};

节点:8.10.0 npm :5.6.0 反应原生:0.59.5

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试执行以下babel配置,如果丢失,不要忘记安装所需的库:

module.exports = {
  presets: ["@babel/preset-flow", "module:metro-react-native-babel-preset"],
  plugins: [
    ["@babel/plugin-proposal-decorators", { legacy: true }],
    ["@babel/plugin-transform-flow-strip-types"],
    ["babel-plugin-react-native-nodeify-hack"],
    "@babel/plugin-syntax-dynamic-import",
    [
      "@babel/plugin-proposal-class-properties",
      {
        loose: true
      }
    ],
    "@babel/plugin-transform-regenerator",
    [
      "@babel/plugin-transform-runtime",
      {
        helpers: false,
        regenerator: true
      }
    ]
  ]
};

然后您可以使用此命令清理项目(仅适用于IOS)

watchman watch-del-all && rm -rf node_modules && npm install && rm -rf $TMPDIR/haste-map-react-native-packager-* && rm -rf $TMPDIR/react-native-packager-cache-* && rm -rf $TMPDIR/metro-bundler-cache-* && rm -rf ~/Library/Developer/Xcode/DerivedData && pod deintegrate ios/{YOUR_PROJECT}.xcodeproj && rm -rf ./ios/Pods && pod install --project-directory=./ios/ && rm -f /ios/build && npm cache verify

然后重新运行项目

react-native run-ios

希望这能解决您的问题