【问题标题】:React Native (Expo) project raises "window.addEventListener is not a function"React Native (Expo) 项目引发“window.addEventListener 不是函数”
【发布时间】:2021-06-24 09:18:24
【问题描述】:

过去一天左右,我一直在尝试对我的项目进行故障排除,但到目前为止没有运气。到目前为止还没有记录我所采取的步骤,但会尽我所能提供一个概述。

昨天我的应用程序运行良好,我去打开远程调试器,应用程序崩溃并出现错误。我认为这没什么,并进行了一段时间的调试。最终我开始收到错误。

window.addEventListener is not a function.
(In 'window.addEventListener("testPassive", null, e)', 'window.addEventListener' is undefined)

我在 Google 上快速搜索了herehere,发现用户在声明 firebase.initalizeApp(config) 时链接了错误。我听从了一些建议。

  • 降级/升级、删除、重新安装 Firebase。 npm install firebase/expo install firebase抛出

        gyp ERR! build error
    
        gyp ERR! stack Error: `C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\MSBuild.exe` failed with exit code: 1
    
        gyp ERR! stack     at ChildProcess.onExit (C:\Users\user\Documents\React\project\node_modules\node-gyp\lib\build.js:194:23)
    
        gyp ERR! stack     at ChildProcess.emit (node:events:369:20)
    
        gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)
    
        gyp ERR! System Windows_NT 10.0.19041
    
        gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\user\\Documents\\React\\project\\node_modules\\node-gyp\\bin\\node-gyp.js" "build" "--fallback-to-build" "--library=static_library" "--module=C:\\Users\\user\\Documents\\React\\project\\node_modules\\grpc\\src\\node\\extension_binary\\node-v88-win32-x64-unknown\\grpc_node.node" "--module_name=grpc_node" "--module_path=C:\\Users\\user\\Documents\\React\\project\\node_modules\\grpc\\src\\node\\extension_binary\\node-v88-win32-x64-unknown" "--napi_version=8" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v88"
    
        gyp ERR! cwd C:\Users\user\Documents\React\project\node_modules\grpc
    
        gyp ERR! node-gyp -v v7.1.2
    
        gyp ERR! not ok
    
        node-pre-gyp ERR! build error
    
        act\project\node_modules\grpc\src\node\extension_binary\node-v88-win32-x64-unknown\grpc_node.node --module_name=grpc_node --module_path=C:\Users\user\Documents\React\project\node_modules\grpc\src\node\extension_binary\node-v88-win32-x64-unknown --napi_version=8 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v88' (1)
    
        node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (C:\Users\user\Documents\React\project\node_modules\node-pre-gyp\lib\util\compile.js:83:29)
    
        node-pre-gyp ERR! stack     at ChildProcess.emit (node:events:369:20)
    
        node-pre-gyp ERR! stack     at maybeClose (node:internal/child_process:1067:16)
    
        node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5)
    
        node-pre-gyp ERR! System Windows_NT 10.0.19041
    
        node-pre-gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\user\\Documents\\React\\project\\node_modules\\node-pre-gyp\\bin\\node-pre-gyp" "install" "--fallback-to-build" "--library=static_library"
    
        node-pre-gyp ERR! cwd C:\Users\user\Documents\React\project\node_modules\grpc
    
        node-pre-gyp ERR! node -v v15.12.0
    
        node-pre-gyp ERR! node-pre-gyp -v v0.14.0
    
        node-pre-gyp ERR! not ok```
    
    
  • 在应用的顶层插入window.addEventListener = x =&gt; x;

  • 降级/升级 Node.js

  • 创建一个新的 Expo 项目,复制组件并从头开始重新安装包

  • 清除 npm 缓存,删除 package-lock.json 并运行 npm install

  • yarn add firebasebuilding fresh packages... grpc waiting... 击中,然后在 5 分钟左右抛出:

       yarnpkg exited with non-zero code: 1
    
       Error: yarnpkg exited with non-zero code: 1
    
         at ChildProcess.completionListener (C:\Users\user\AppData\Roaming\npm\node_modules\expo-cli\node_modules\@expo\spawn-async\src\spawnAsync.ts:65:13)
    
         at Object.onceWrapper (node:events:476:26)
    
         at ChildProcess.emit (node:events:369:20)
    
         at ChildProcess.cp.emit (C:\Users\user\AppData\Roaming\npm\node_modules\expo-cli\node_modules\cross-spawn\lib\enoent.js:34:29)
    
         at maybeClose (node:internal/child_process:1067:16)
    
         at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5)
    
         ...
    
         at Object.spawnAsync [as default] (C:\Users\user\AppData\Roaming\npm\node_modules\expo-cli\node_modules\@expo\spawn-async\src\spawnAsync.ts:26:19)
    
         at YarnPackageManager._runAsync (C:\Users\user\AppData\Roaming\npm\node_modules\expo-cli\node_modules\@expo\package-manager\src\NodePackageManagers.ts:301:31)
    
         at YarnPackageManager.addAsync (C:\Users\user\AppData\Roaming\npm\node_modules\expo-cli\node_modules\@expo\package-manager\src\NodePackageManagers.ts:254:16)
    
         at installAsync (C:\Users\user\AppData\Roaming\npm\node_modules\expo-cli\src\commands\install.ts:130:3)
    
         at Command.<anonymous> (C:\Users\user\AppData\Roaming\npm\node_modules\expo-cli\src\exp.ts:349:7)```
    
  • yarn add firebase 也抛出:

    warning firebase &gt; @firebase/firestore &gt; grpc &gt; node-pre-gyp@0.14.0: Please upgrade to @mapbox/node-pre-gyp: the non-scoped node-pre-gyp package is deprecated and only the @mapbox scoped package will recieve updates in the future

  • yarn add @mapbox/node-pre-gyp 似乎没有使警告消失并引发相同的错误

  • 禁用防火墙,运行安装

  • 使用--force 命令/标志运行安装

其他项目似乎启动良好。我完全不知道这里发生了什么。

【问题讨论】:

    标签: firebase react-native expo


    【解决方案1】:

    插入 window.addEventListener = x => x;位于应用的顶层。

    React Native 中没有全局 window.addEventListener 函数。执行环境不同于网络浏览器。

    (安装 Firebase 时出错)

    您可以安装firebase@8.2.3 - yarn add firebase@8.2.3,我相信他们已经删除了该版本中的 grpc 依赖项。由于各种原因,在 Windows 机器上为 Node 包构建原生依赖项通常会失败,您应该在与这些包相关的存储库中搜索问题以获取更多信息。例如:https://github.com/grpc/grpc-node/issues/1183

    【讨论】:

    • 非常感谢!这解决了问题。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 2023-04-06
    • 2021-11-07
    • 2019-11-16
    • 2020-04-05
    • 2018-12-23
    • 1970-01-01
    相关资源
    最近更新 更多