【问题标题】:Unable to resolve module `@babel/runtime/helpers/interopRequireDefault`无法解析模块`@babel/runtime/helpers/interopRequireDefault`
【发布时间】:2018-09-24 19:37:05
【问题描述】:

使用标准 react-native init MyApp 创建新的 react native 项目并第一次运行 react-native run-ios 时,我看到以下错误

error: bundling failed: Error: Unable to resolve module `@babel/runtime/helpers/interopRequireDefault` from `/Users/chrisedgington/Development/ReactNative/SixNationsPredictor/index.js`: Module `@babel/runtime/helpers/interopRequireDefault` does not exist in the Haste module map

This might be related to https://github.com/facebook/react-native/issues/4968
To resolve try the following:
  1. Clear watchman watches: `watchman watch-del-all`.
  2. Delete the `node_modules` folder: `rm -rf node_modules && npm install`.
  3. Reset Metro Bundler cache: `rm -rf /tmp/metro-bundler-cache-*` or `npm start -- --reset-cache`.
  4. Remove haste cache: `rm -rf /tmp/haste-map-react-native-packager-*`.
    at ModuleResolver.resolveDependency (/Users/chrisedgington/Development/ReactNative/MyApp/node_modules/metro/src/node-haste/DependencyGraph/ModuleResolution.js:209:1301)
    at ResolutionRequest.resolveDependency (/Users/chrisedgington/Development/ReactNative/MyApp/node_modules/metro/src/node-haste/DependencyGraph/ResolutionRequest.js:83:16)
    at DependencyGraph.resolveDependency (/Users/chrisedgington/Development/ReactNative/MyApp/node_modules/metro/src/node-haste/DependencyGraph.js:238:485)
    at Object.resolve (/Users/chrisedgington/Development/ReactNative/MyApp/node_modules/metro/src/lib/transformHelpers.js:180:25)
    at dependencies.map.result (/Users/chrisedgington/Development/ReactNative/MyApp/node_modules/metro/src/DeltaBundler/traverseDependencies.js:311:29)
    at Array.map (<anonymous>)
    at resolveDependencies (/Users/chrisedgington/Development/ReactNative/MyApp/node_modules/metro/src/DeltaBundler/traverseDependencies.js:307:16)
    at /Users/chrisedgington/Development/ReactNative/MyApp/node_modules/metro/src/DeltaBundler/traverseDependencies.js:164:33
    at Generator.next (<anonymous>)
    at step (/Users/chrisedgington/Development/ReactNative/MyApp/node_modules/metro/src/DeltaBundler/traverseDependencies.js:266:307)

我已尝试运行建议但仍然看到相同的问题。我看过一些关于类似问题的帖子,但似乎没有具体说明如何解决 react-native 中的问题。

macOS: 10.13.6 
node: 8.11.3
react-native-cli: 2.0.1
react-native: 0.57.1

【问题讨论】:

    标签: react-native


    【解决方案1】:

    试一试:

    npm add @babel/runtime

    或者升级 babel 运行时:

    "@babel/runtime": "7.0.0-beta.55"

    【讨论】:

    • 自 2 天以来一直在用这个错误敲我的头。在 2 分钟内得到解决...非常感谢@jrk
    • @CKT - 这就是 stackoverflow 的用途:)
    • @th3g3ntl3m3n 确保在安装软件包后重新启动 Metro Builder。
    • 安装 @babel/runtime 后,它对我有用 npm start -- --reset-cache
    【解决方案2】:

    执行前应先退出地铁终端

    npm add @babel/runtime
    npm install
    

    【讨论】:

      【解决方案3】:

      你应该为你的项目添加和安装 babel

      npm add @babel/runtime
      npm install
      

      如果错误未修复,请尝试:

      npm start --reset-cache
      

      【讨论】:

      • 就我而言,这解决了问题npm start --reset-cache
      • 这对我有用!谢谢!
      【解决方案4】:

      尝试先更新你的 npm 版本

      npm update -g npm@versionsudo npm -gf update npm@version

      然后在你的 react native 项目中添加 babel 运行时

      npm add @babel/runtime

      【讨论】:

        【解决方案5】:

        尝试升级您的软件包。您可能有一个导致问题的旧包:

        yarn upgrade-interactive --latest
        

        【讨论】:

          【解决方案6】:

          对我来说,问题是 @babel/runtime 被安装为开发依赖而不是普通(非开发)依赖

          【讨论】:

            【解决方案7】:

            当前错误消息建议采取以下步骤来解决此问题:

            1. 清空守望者手表:watchman watch-del-all
            2. 删除 node_modules:rm -rf node_modules 并运行 yarn install
            3. 重置 Metro 的缓存:yarn start --reset-cache
            4. 删除缓存:rm -rf /tmp/metro-*

            最后一个帮我解决了。

            【讨论】:

              【解决方案8】:

              我今天(2021 年 4 月)遇到了这个问题,我只能通过从我的 webpack 配置中删除 webpack-node-externals 包来解决它。

              【讨论】:

                【解决方案9】:

                对我来说,解决方案是(Mac):

                1. 停止 IntelliJ
                2. 进入终端
                3. 运行:npx browserslist@latest --update-db -g
                4. 运行:npm cache verify
                5. 启动 IntelliJ

                如果还是不行:

                1. 如果存在,则从项目中删除 node_modules
                2. 运行:npm cache verify
                3. 在项目中运行 npm install -f
                4. 运行:npx browserslist@latest --update-db -g

                【讨论】:

                  【解决方案10】:

                  问题:

                  我正在升级flow到typescript,我使用@khanacademy/flow-to-ts将js文件转换为ts。该lib不仅改变了应用的js文件,还转换了node_modules目录下的所有js文件。

                  解决方案:

                  我不得不删除node_modulesnpm -i。这次当我检查node_modules 时,interopRequireDefault.js 文件就在那里。

                  【讨论】:

                    【解决方案11】:

                    如果上面列出的解决方案都不适合我,我必须去node_modules/jest-haste-map/build/index.js

                    更改为const crawl = canUseWatchman &amp;&amp; this._options.useWatchman ? _watchman.default : _node.default;

                    const crawl = canUseWatchman &amp;&amp; this._options.useWatchman ? _node.default : _node.default;

                    【讨论】:

                    • 更改 node_modules 文件夹中的某些内容绝不是一个好主意..
                    猜你喜欢
                    • 2021-11-14
                    • 1970-01-01
                    • 2019-06-21
                    • 1970-01-01
                    • 2022-12-13
                    • 1970-01-01
                    • 2021-11-18
                    • 2021-05-29
                    • 2022-06-10
                    相关资源
                    最近更新 更多