Search code examples
node.jsnode.js-stream

In node.js, Buffer.from() method is throwing "ascii is not a function" error


In Node.js, Buffer class is global. When I am trying to execute below line of code in Visual Studio code, node.js is throwing exception.

My Code

var buffer = Buffer.from('Hello','ascii');

Exception

TypeError: ascii is not a function
    at Function.from (native)
    at Function.from (native)

Solution

  • The Buffer.from() you're seeing is actually ArrayBuffer.from() which has different behavior. Fortunately for LTS users, node v4.5.0 was recently released which does include the new node Buffer APIs (including the custom Buffer.from()). Also, node v6.x (which will become the next LTS branch in October) has had these APIs for awhile now.