TypeError Base64 is not a function when using Buff

2019-02-24 05:07发布

问题:

I am debugging a lambda function locally on Node 4.3 using the standard Amazon Machine Image (linux x64)

When I run the program, I get an error in a function that is meant to decode a base64 string to utf-8. The error says

Unhandled rejection TypeError: base64 is not a function
    at Function.from (native)
    at Function.from (native)
    at /home/ec2-user/sass-compiler/lib/compiler.module.js:26:30

My code seems rather simple. I have checked around SO and the node 4.3 docs and my code seems compliant. Any ideas what I might be doing wrong?

template() {
        return new Promise((resolve, reject) => {
            let buf = Buffer.from(this._opts.tpl, 'base64').toString('utf-8');
            let _tpl = _.template(buf);
            resolve(_tpl(this._opts.opts));
        });
    }

EDIT The program works just fine on Windows 10 using Node LTS 6.9.5

回答1:

I finally found an answer on Stack Overflow that answers this question.

NodeJS: How to decode base64 encoded string back to binary?

In node 5.3 & below, you need to use the buffer constructor (ie: new Buffer) instead of Buffer.from