Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError Base64 is not a function when using Buffer.from in Node 4.3

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

like image 336
Taylor Ackley Avatar asked Mar 09 '23 20:03

Taylor Ackley


1 Answers

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

like image 149
Taylor Ackley Avatar answered Apr 24 '23 20:04

Taylor Ackley