web3 / web3.js

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.
https://web3js.org/
Other
19.15k stars 4.9k forks source link

Can't find variable: atob #5656

Closed SurpriseMF3000 closed 6 months ago

SurpriseMF3000 commented 1 year ago

Is there an existing issue for this?

Current Behavior

Hello, im trying to use web3.js in my React project but receive the error Can't find variable: atob Implementing and adding web3.js via yarn works fine but the import in the project doesn't work. Bug Version: web3 1.8.1 Working Version: web3 1.8.0

Thanks for Help;)

Expected Behavior

A working web3.js 1.8.1

Steps to Reproduce

console: yarn add web3

App.js: import Web3 from 'web3';

Web3.js Version

1.8.1

Environment

Anything Else?

No response

SurpriseMF3000 commented 1 year ago

Like I said: web3.js 1.8.0 works perfectly

luu-alex commented 1 year ago

thank you for reporting this issue, can you provide some extra information? Node.js version, NPM Version and the contents within your package.json?

SurpriseMF3000 commented 1 year ago

Node version: 16.14.2 Yarn Version: 1.22.19

filipesmedeiros commented 1 year ago

Is it running in the browser or server? It should be available natively on both, weird

SurpriseMF3000 commented 1 year ago

Compiled Android App 1.8.0 works just perfect

filipesmedeiros commented 1 year ago

Ok I thought this was just for web envs. Sorry

luu-alex commented 1 year ago

What are you using to build the android app?

SurpriseMF3000 commented 1 year ago

Yarn Version: 1.22.19

nithronium commented 1 year ago

Does 1.8.0 include anything with atob?

The issue happens because Android & iOS devices doesn't have atob and btoa functions natively. If you try to run the app on debug mode, it uses Chrome's JS engine so app works perfectly fine. On the other hand, the web3.min.js & index.js files include atob function, which is unknown to Android (and is deprecated on JS as well).

For anyone having this issue, you can add;

var Buffer = require('buffer/').Buffer;
function atob(str) {
  return Buffer.from(str, 'base64').toString('binary');
}
function btoa(str) {
  return Buffer.from(str, 'binary').toString('base64');
}

at the beginning of your index.js or web3.min.js file under the node_modules\web3\dist folders, and it would compile and run perfectly fine.

kitdesai commented 1 year ago

thanks @nithronium, that fixed it for me (updating to 1.8.0 didn't help)

luu-alex commented 1 year ago

@nithronium thank you for this, i'd like to try to fix this for upcoming versions. would you happen to know if android apps were working properly before 1.8?

nithronium commented 1 year ago

@nithronium thank you for this, i'd like to try to fix this for upcoming versions. would you happen to know if android apps were working properly before 1.8?

The code base I was using was old and I'm rewriting the project. However in the previous issues too I think it wasn't working because previous devs had to declare them on the globals.js file.

mconnelly8 commented 1 year ago

Hey @SurpriseMF3000 , please use the suggestion mentioned here: https://github.com/web3/web3.js/issues/5656#issuecomment-1401028231.

We will be investigating this in the 4.x version to see if it's an issue, but will not be making any 1.x updates for it.

mconnelly8 commented 1 year ago

Need to investigate in 4.x to validate an issue or not.

mconnelly8 commented 6 months ago

Hey @SurpriseMF3000 & @nithronium, 1.x will be deprecated at the end of March 2024. We encourage everyone to upgrade to the 4.x as soon as possible. If you experience this issue in 4.x, please let us know.