未处理的 JS 异常:找不到变量:需要

Posted

技术标签:

【中文标题】未处理的 JS 异常:找不到变量:需要【英文标题】:Unhandled JS Exception: Can't find variable: require 【发布时间】:2019-02-28 09:19:53 【问题描述】:

React 原生版本:0.57.3

android:Google Pixel 3 API - 28

ios:iPhone 8 plus - 11.4

react-native-web: 0.10.0

我正在尝试运行在网络上运行良好的 react-native-web 应用程序。在尝试解决移动端的 babel 插件问题后,我遇到了这个问题。我使用 babel-upgrade 进行自动 babel 升级。我在这里发布我的 package.json 和 babelrc。

package.json:

"devDependencies": 
        "@babel/cli": "^7.0.0",
        "@babel/core": "^7.0.0",
        "@babel/plugin-syntax-dynamic-import": "^7.0.0",
        "@babel/preset-env": "^7.0.0",
        "@babel/preset-flow": "^7.0.0",
        "@babel/preset-react": "^7.0.0",
        "babel-eslint": "^9.0.0",
        "babel-jest": "^23.4.2",
        "babel-loader": "8.0.0-beta.0",
        "babel-plugin-module-resolver": "3.2.0",
        "babel-plugin-react-native-web": "0.8.4",
        "babel-preset-react-native": "5.0.2",
        "jest": "23.2.0",
        "metro": "^0.52.0",
        "metro-core": "^0.52.0"
    ,
    "dependencies": 
        "@babel/core": "^7.0.0",
        "@babel/plugin-proposal-class-properties": "^7.0.0",
        "@babel/plugin-proposal-decorators": "^7.0.0",
        "@babel/plugin-proposal-do-expressions": "^7.0.0",
        "@babel/plugin-proposal-export-default-from": "^7.0.0",
        "@babel/plugin-proposal-export-namespace-from": "^7.0.0",
        "@babel/plugin-proposal-function-bind": "^7.0.0",
        "@babel/plugin-proposal-function-sent": "^7.0.0",
        "@babel/plugin-proposal-json-strings": "^7.0.0",
        "@babel/plugin-proposal-logical-assignment-operators": "^7.0.0",
        "@babel/plugin-proposal-nullish-coalescing-operator": "^7.0.0",
        "@babel/plugin-proposal-numeric-separator": "^7.0.0",
        "@babel/plugin-proposal-optional-chaining": "^7.0.0",
        "@babel/plugin-proposal-pipeline-operator": "^7.0.0",
        "@babel/plugin-proposal-throw-expressions": "^7.0.0",
        "@babel/plugin-syntax-dynamic-import": "^7.0.0",
        "@babel/plugin-syntax-import-meta": "^7.0.0",
        "@babel/polyfill": "^7.0.0",
        "@babel/preset-env": "^7.0.0",
        "@babel/preset-react": "^7.0.0",
        "@sentry/browser": "4.5.3",
        "axios": "0.18.0",
        "babel-plugin-module-resolver": "3.2.0",
        "babel-polyfill": "^6.26.0"
    

.babelrc:

"presets": [
    "@babel/env",
    "@babel/preset-react",
    "react-native",
    "@babel/preset-flow",
    "module:metro-react-native-babel-preset"
],
"plugins": [
    "@babel/plugin-transform-async-to-generator",
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-syntax-import-meta",
    "@babel/plugin-proposal-class-properties",
    "@babel/plugin-proposal-json-strings",
    [
        "@babel/plugin-proposal-decorators",
        
            "legacy": true
        
    ],
    "@babel/plugin-proposal-function-sent",
    "@babel/plugin-proposal-export-namespace-from",
    "@babel/plugin-proposal-numeric-separator",
    "@babel/plugin-proposal-throw-expressions",
    "@babel/plugin-proposal-export-default-from",
    "@babel/plugin-proposal-logical-assignment-operators",
    "@babel/plugin-proposal-optional-chaining",
    [
        "@babel/plugin-proposal-pipeline-operator",
        
            "proposal": "minimal"
        
    ],
    "@babel/plugin-proposal-nullish-coalescing-operator",
    "@babel/plugin-proposal-do-expressions",
    "@babel/plugin-proposal-function-bind"
],
"sourceMaps": true 

这些是我迄今为止尝试过但没有奏效的解决方案:

1) react-native start --reset-cache

2) 删除所有 node_modules 并重新安装它们

【问题讨论】:

【参考方案1】:

将您的 .babelrc 文件更改为:


  "presets": [ "module:metro-react-native-babel-preset"  ],
  "sourceMaps": true

.babelrc 文件中删除其他内容。 因为其他预设和插件对 React 有效,而对 react-native 则不需要。

完成此操作后,通过执行以下命令启动打包程序并重置缓存。

react-native start --reset-cache

Please refer this GitHub comments

【讨论】:

以上是关于未处理的 JS 异常:找不到变量:需要的主要内容,如果未能解决你的问题,请参考以下文章

角度:发生未处理的异常:找不到模块'webpack'?

Angular:发生未处理的异常:找不到模块'webpack/lib/ParserHelpers'

发生未处理的异常:找不到模块'@angular-devkit/build-angular/package.json'

未找到Android活动异常:找不到处理Intent的活动

未处理的异常:在 _WidgetsAppState 中找不到路由 RouteSettings("/CatPage", null) 的生成器

找不到网络路径