React Native atob() / btoa() not working without remote JS debugging

That’s the ways I fixed it. As @chemitaxis suggests, add base-64 module from NPM:

npm i -S base-64

Based on it, I propose a couple of ways to use it:

Importing it in files you need it

Then, you can import ‘encode’ and ‘decode’ methods using aliases, this way:

import {decode as atob, encode as btoa} from 'base-64'

Of course, using aliases is optional.

Polyfill way

You can set atob and btoa as global variables on React Native. Then, you won’t need to import them on each file you need it. You have to add this code:

import {decode, encode} from 'base-64'

if (!global.btoa) {
    global.btoa = encode;
}

if (!global.atob) {
    global.atob = decode;
}

You need to place it at the beginning of your index.js, so that it can be loaded before another file uses atob and btoa.

I suggest you to copy it on a separate file (let’s say base64Polyfill.js), and then import it on index.js

Leave a Comment