Node.js throws “btoa is not defined” error

后端 未结 10 1587
悲&欢浪女
悲&欢浪女 2020-12-02 04:40

In my node.js application I did an npm install btoa-atob so that I could use the btoa() and atob() functions which are native in clien

10条回答
  •  悲哀的现实
    2020-12-02 04:50

    My team ran into this problem when using Node with React Native and PouchDB. Here is how we solved it...

    NPM install buffer:

    $ npm install --save buffer
    

    Ensure Buffer, btoa, and atob are loaded as a globals:

    global.Buffer = global.Buffer || require('buffer').Buffer;
    
    if (typeof btoa === 'undefined') {
      global.btoa = function (str) {
        return new Buffer(str, 'binary').toString('base64');
      };
    }
    
    if (typeof atob === 'undefined') {
      global.atob = function (b64Encoded) {
        return new Buffer(b64Encoded, 'base64').toString('binary');
      };
    }
    

提交回复
热议问题