在node.js中,Buffer.from()方法正在抛出“ascii不是函数”的错误

在Node.js中,Buffer类是全局的。 当我试图在Visual Studio代码中执行下面的代码行时,node.js抛出exception。

我的代码

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

例外

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

您看到的Buffer.from()实际上是具有不同行为的ArrayBuffer.from() 。 幸运的是,对于LTS用户,最近发布了节点v4.5.0 ,其中包括新的节点Buffer API(包括自定义的Buffer.from() )。 此外,节点v6.x(将在十月成为下一个LTS分支)现在已经有了这些API。