Getting "TypeError: undefined is not an object (evaluating '_ExpoKeepAwake.default.activate')" after upgrading to Expo 41

Please provide the following:

  1. SDK Version: 41
  2. Platforms(Android/iOS/web/all): iOS
  3. Add the appropriate “Tag” based on what Expo library you have a question on.
  4. Workflow: Bare

After upgrading to Expo 41 I’m getting the following error as soon as my app loads:
TypeError: undefined is not an object (evaluating '_ExpoKeepAwake.default.activate').

Full error with stack trace:

 ERROR  TypeError: undefined is not an object (evaluating '_ExpoKeepAwake.default.activate')

This error is located at:
    in ExpoRoot (at renderApplication.js:47)
    in RCTView (at View.js:34)
    in View (at AppContainer.js:107)
    in RCTView (at View.js:34)
    in View (at AppContainer.js:134)
    in AppContainer (at renderApplication.js:40)

This error is not coming from my code. I’m not directly using expo-keep-awake anywhere in my code. It’s not even listed in my package.json file. I was able to make the error go away by making the following manual edits to the expo package in node_modules/ directory:

// node_modules/expo/build/launch/withExpoRoot.js

import * as ErrorRecovery from 'expo-error-recovery';
import * as React from 'react';
// This hook can be optionally imported because __DEV__ never changes during runtime.
// Using __DEV__ like this enables tree shaking to remove the hook in production.

// Comment out all of this code
//let useDevKeepAwake = () => { };
//if (__DEV__) {
//    try {
//        // Optionally import expo-keep-awake
//        const { useKeepAwake } = require('expo-keep-awake');
//        useDevKeepAwake = useKeepAwake;
//    }
//    catch { }
//}
export default function withExpoRoot(AppRootComponent) {
    return function ExpoRoot(props) {
// Comment out this line too
//        useDevKeepAwake();
        const combinedProps = {
            ...props,
            exp: { ...props.exp, errorRecovery: ErrorRecovery.recoveredProps },
        };
        return React.createElement(AppRootComponent, Object.assign({}, combinedProps));
    };
}
//# sourceMappingURL=withExpoRoot.js.map

Looking at the stack trace, it looks like this is where the error is occurring:

// node_modules/expo-keep-awake/src/index.ts

export function activateKeepAwake(tag: string = ExpoKeepAwakeTag): void {
  // ExpoKeepAwake is undefined here
  if (ExpoKeepAwake.activate) ExpoKeepAwake.activate(tag);
}

I can also make this error go away by running the app in Release mode rather than Debug mode, because that sets __DEV__ equal to false, but then I lose hot-reloading.

I’m running on a physical iPhone (not a simulator) using the latest version of Xcode. I’m running the latest version of iOS 14 on my device. I’ve tried all of the obvious fixes like rm -rf node_modules && npm install, clearing my build folder in Xcode, and clearing the Metro server cache. Here’s my package.json file:

{
  "scripts": {
    "start": "react-native start",
    "test": "jest",
    "test-debug": "node --inspect node_modules/.bin/jest --watch --runInBand",
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "@apollo/client": "^3.3.7",
    "@react-native-community/datetimepicker": "^3.4.6",
    "@react-native-community/masked-view": "^0.1.10",
    "@react-native-community/netinfo": "6.0.0",
    "@react-navigation/bottom-tabs": "^5.11.7",
    "@react-navigation/native": "^5.9.2",
    "@react-navigation/stack": "^5.14.2",
    "@sentry/react-native": "^2.4.0",
    "amazon-cognito-identity-js": "^4.5.7",
    "aws-amplify": "^3.3.14",
    "babel-preset-expo": "^8.3.0",
    "expo": "^41.0.0",
    "expo-font": "~9.1.0",
    "expo-location": "~12.0.4",
    "expo-splash-screen": "~0.10.2",
    "expo-status-bar": "~1.0.4",
    "expo-updates": "~0.5.4",
    "graphql": "^15.5.0",
    "prop-types": "^15.7.2",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-native": "^0.64.0",
    "react-native-agora": "^3.2.2",
    "react-native-elements": "^3.3.2",
    "react-native-gesture-handler": "~1.10.2",
    "react-native-get-random-values": "~1.7.0",
    "react-native-reanimated": "~2.1.0",
    "react-native-safe-area-context": "3.2.0",
    "react-native-screens": "~3.0.0",
    "react-native-unimodules": "~0.13.3",
    "react-native-web": "^0.15.7",
    "rn-range-slider": "^2.0.4",
    "subscriptions-transport-ws": "^0.9.18",
    "uuid": "^8.3.2"
  },
  "devDependencies": {
    "@babel/core": "~7.9.0",
    "@testing-library/jest-native": "^3.4.3",
    "@testing-library/react-native": "^7.1.0",
    "babel-jest": "~25.2.6",
    "eslint": "^7.18.0",
    "eslint-plugin-jest": "^24.1.3",
    "eslint-plugin-react": "^7.22.0",
    "eslint-plugin-react-hooks": "^4.2.0",
    "eslint-plugin-testing-library": "^3.10.1",
    "identity-obj-proxy": "^3.0.0",
    "jest": "~25.2.6",
    "promise-polyfill": "^8.2.0",
    "react-test-renderer": "~16.13.1"
  },
  "private": true,
  "name": "redacted",
  "version": "1.0.0"
}

It’s defined near the top of the file:

I think this workaround should be enough, although of course it should not be necessary:

--- node_modules/expo/build/launch/withExpoRoot.js	2021-04-17 20:28:25.466000000 +0200
+++ node_modules/expo/build/launch/withExpoRoot.js	2021-04-18 08:55:38.398585154 +0200
@@ -3,7 +3,7 @@
 // This hook can be optionally imported because __DEV__ never changes during runtime.
 // Using __DEV__ like this enables tree shaking to remove the hook in production.
 let useDevKeepAwake = () => { };
-if (__DEV__) {
+if (false && __DEV__) {
     try {
         // Optionally import expo-keep-awake
         const { useKeepAwake } = require('expo-keep-awake');

Do you get the same error with a newly created app? Or if you create a new app, install all the dependencies and then eject?

I tried starting a new project with expo init then ejecting and upgrading my dependencies, but I couldn’t reproduce the error. I can live with the workaround for now, but if I discover anything else, I’ll update this thread.

it sounds like the native module for expo-keep-awake is missing. i’d try clearing your lockfile and node_modules, reinstalling, then running pod install again / building your ios/android projects as a brute force fix

1 Like

That fixed it, thanks @notbrent! I deleted package-lock.json and Podfile.lock and looking at the diff after running npm install and npx pod-install, it looks like the issue was in my Podfile.lock file. After deleting the file and re-running npx pod-install, this is the diff that was generated.

diff --git a/ios/Podfile.lock b/ios/Podfile.lock
index 3ea8fa6..5a44b1c 100644
--- a/ios/Podfile.lock
+++ b/ios/Podfile.lock
@@ -2,9 +2,13 @@ PODS:
   - AgoraRtcEngine_iOS (3.3.2)
   - boost-for-react-native (1.63.0)
   - DoubleConversion (1.1.6)
+  - EXApplication (3.1.2):
+    - UMCore
   - EXConstants (10.1.3):
     - UMConstantsInterface
     - UMCore
+  - EXErrorRecovery (2.1.0):
+    - UMCore
   - EXFileSystem (11.0.2):
     - UMCore
     - UMFileSystemInterface
@@ -15,6 +19,8 @@ PODS:
     - React-Core
     - UMCore
     - UMImageLoaderInterface
+  - EXKeepAwake (9.1.2):
+    - UMCore
   - EXLocation (12.0.4):
     - UMCore
     - UMPermissionsInterface
@@ -377,10 +383,13 @@ PODS:
 
 DEPENDENCIES:
   - DoubleConversion (from `../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec`)
+  - EXApplication (from `../node_modules/expo-application/ios`)
   - EXConstants (from `../node_modules/expo-constants/ios`)
+  - EXErrorRecovery (from `../node_modules/expo-error-recovery/ios`)
   - EXFileSystem (from `../node_modules/expo-file-system/ios`)
   - EXFont (from `../node_modules/expo-font/ios`)
   - EXImageLoader (from `../node_modules/expo-image-loader/ios`)
+  - EXKeepAwake (from `../node_modules/expo-keep-awake/ios`)
   - EXLocation (from `../node_modules/expo-location/ios`)
   - EXPermissions (from `../node_modules/expo-permissions/ios`)
   - EXSplashScreen (from `../node_modules/expo-splash-screen/ios`)
@@ -449,14 +458,20 @@ SPEC REPOS:
 EXTERNAL SOURCES:
   DoubleConversion:
     :podspec: "../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec"
+  EXApplication:
+    :path: "../node_modules/expo-application/ios"
   EXConstants:
     :path: "../node_modules/expo-constants/ios"
+  EXErrorRecovery:
+    :path: "../node_modules/expo-error-recovery/ios"
   EXFileSystem:
     :path: "../node_modules/expo-file-system/ios"
   EXFont:
     :path: "../node_modules/expo-font/ios"
   EXImageLoader:
     :path: "../node_modules/expo-image-loader/ios"
+  EXKeepAwake:
+    :path: "../node_modules/expo-keep-awake/ios"
   EXLocation:
     :path: "../node_modules/expo-location/ios"
   EXPermissions:
@@ -574,10 +589,13 @@ SPEC CHECKSUMS:
   AgoraRtcEngine_iOS: 851e11fa7cbc0cba04f407eac1a9e32b872765a1
   boost-for-react-native: 39c7adb57c4e60d6c5479dd8623128eb5b3f0f2c
   DoubleConversion: cf9b38bf0b2d048436d9a82ad2abe1404f11e7de
+  EXApplication: 4797b8b37f0b0470f587fdccf6407f44b50d18b5
   EXConstants: c4dd28acc12039c999612507a5f935556f2c86ce
+  EXErrorRecovery: 720641265b8cf95e6cdeb1884ac38e794a352488
   EXFileSystem: dcf2273f49431e5037347c733a2dc5d08e0d0a9e
   EXFont: d6fb79f9863120f0d0b26b0c2d1453bc9511e9df
   EXImageLoader: da941c9399e01ec28f2d5b270bdd21f2c8ca596c
+  EXKeepAwake: d4e4a3ed8c1c4fd940dd62fc5a8be2a190371fd4
   EXLocation: 65d4250e06ae1c59a3b4aa5d93aa36b0eed8ad7b
   EXPermissions: 8f8c1c05580c4e02d4ee2c8dd74bfe173ff6a723
   EXSplashScreen: a9baaf4fa866003884c90ba049f18760d6a8ce39

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.