axios: Can't find variable: btoa

Describe the bug Using Basic Auth with axios in an expo app causes this error. It was working before upgrading both axios and expo, not sure what is causing the issue but seems btoa function is not available in expo envirorment.

To Reproduce Make a get request with auth headers from an expo app

let axiosInstance = axios.create({
  baseURL: 'http://example.com',
  auth: {
    username: 'user',
    password: 'password'
  }
});
axiosIstance.get('/path/to');

Environment:

  • Axios Version 0.18.0
  • expo 32.0.0
  • react 16.5

Additional context/Screenshots Schermata 2019-06-16 alle 21 52 09

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 14
  • Comments: 21

Commits related to this issue

Most upvoted comments

Hi, I’m new in GitHub, however let me try to help. Solved the current issue (Axios Removed btoa variable Polyfill) with these steps: (Used in React-Native Project)

  1. Install base-64 npm package, npmjs.com/package/base-64 ,
  2. 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. Add the following at beginning of your index.js, so that it can be loaded before another file uses atob and btoa:

import {decode, encode} from ‘base-64’

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

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

Source : https://stackoverflow.com/questions/42829838/react-native-atob-btoa-not-working-without-remote-js-debugging.

Hopefully Helps.

Thanks.

put “axios”: “0.18.0” in project dependencies that’s worked for me

@Alanscut @SyneticDevOps According to #1689, it is not a bug, but an intentional change. If you want an out-of-box usage, you have to lock axios to ~0.18.0. Or you should add global btoa polyfill by yourself.

@chinesedfan What is your team’s intention to remove btoa from Axios?

Well, seems like the btoa polyfill has been removed, which causes this issue: https://github.com/axios/axios/compare/v0.18.0...v0.19.0

Also removed in v0.18.1 https://github.com/axios/axios/compare/v0.18.0...v0.18.1

I’m experiencing this same bug in axios 0.19.0. Downgrading to 0.18.0 seems to help for now.