Web: TypeError when navigator.getBattery() resolves with null

Author: olivier-bouilletCreated Apr 2, 2026Updated Aug 1, 2026

Bug Description

On certain browsers/environments, navigator.getBattery() can resolve with null instead of a BatteryManager object. The web module does not guard against this, causing an unhandled TypeError.

Error:

TypeError: Cannot read properties of null (reading 'charging')

Stacktrace:

react-native-device-info/lib/module/web/index.js:60:31 (navigator.getBattery.then$argument_0)
    batteryCharging = battery.charging;

Sentry Report

We received this error via a Sentry alert from the following user agent:

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36

This is Chrome 90 on Linux x86_64. The error was an unhandled promise rejection (onunhandledrejection).

Why getBattery() resolves with null

The W3C Battery Status API spec states that navigator.getBattery() should return a Promise<BatteryManager> — it should either resolve with a BatteryManager object or reject the promise. However, in practice:

  • Linux systems without a physical battery (desktops, servers, VMs, containers): Chromium reads from /sys/class/power_supply/. When no battery device is found, some Chrome versions resolve the promise with null instead of rejecting it. This is a known Chromium implementation quirk (see Chromium bug 122593).
  • Chrome 90 (released April 2021) still fully supported the Battery Status API and exposed navigator.getBattery, so the library's typeof navigator !== 'undefined' && navigator.getBattery check passes — but the resolved value is null.
  • Firefox removed the Battery Status API entirely in Firefox 52 (not affected since navigator.getBattery won't exist).
  • Safari never implemented the API (also not affected).

This primarily affects Chrome/Chromium on Linux desktops without battery hardware.

Affected Code

There are 4 places in src/web/index.ts where navigator.getBattery() is called and the resolved battery object is used without a null check:

  1. init() function — battery.charging, battery.addEventListener(...)
  2. getBatteryLevel() — battery.level
  3. isBatteryCharging() — battery.charging
  4. getPowerState() — _readPowerState(battery)

Suggested Fix

Add a null guard after the promise resolves in all 4 locations:

javascript
// init()
const init = () => {
  if (typeof navigator === 'undefined' || !navigator.getBattery) return;
  navigator.getBattery().then(battery => {
    if (!battery) return;  // guard against null
    batteryCharging = battery.charging;
    // ...
  });
};

// getBatteryLevel()
export const getBatteryLevel = async () => {
  if (navigator.getBattery) {
    const battery = await navigator.getBattery();
    if (!battery) return -1;  // guard against null
    return battery.level;
  }
  return -1;
};

// Same pattern for isBatteryCharging() and getPowerState()

Environment

  • Library version: 15.0.2 (also affects 15.0.1 and likely all prior versions)
  • Platform: Web
  • Browser: Chrome 90.0.4430.93 on Linux x86_64
  • Error source: Sentry alert (unhandled promise rejection via onunhandledrejection)

Source: react-native-device-info/react-native-device-info