NodeJS运行下列代码时报错

var numbers = Buffer("123456789");

(node:3872) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

文档:

https://www.nodeapp.cn/deprecations.html#deprecations_dep0005_buffer_constructor

DEP0005:Buffer()构造函数#

类型:仅限文档

由于API可用性问题而不推荐使用该Buffer()函数和new Buffer()构造函数,这可能会导致意外的安全问题。

作为替代方案,Buffer强烈建议使用以下构造对象的方法:

  • Buffer.alloc(size[, fill[, encoding]])] alloc - Buffer使用 初始化内存创建一个。
  • Buffer.allocUnsafe(size)-创建Buffer未初始化的 内存。
  • Buffer.allocUnsafeSlow(size)-创建Buffer未初始化的 内存。
  • Buffer.from(array)- 创建Buffer一个副本array
  • Buffer.from(arrayBuffer[, byteOffset[, length]])] from_arraybuffer - 创建一个Buffer 包装给定的arrayBuffer
  • Buffer.from(buffer)- 创建Buffer副本buffer
  • Buffer.from(string[, encoding])- 创建Buffer副本 string

解决实现:

var numbers = Buffer.from("123456789");

不再报错

结论:

new Buffer() 和Buffer()函数已经废弃

类方法:Buffer.from(buffer)#

新增于: v5.10.0

  • buffer <Buffer> 一个要拷贝数据的已存在的 Buffer

将传入的 buffer 数据拷贝到一个新建的 Buffer 实例。

例子:

const buf1 = Buffer.from('buffer');
const buf2 = Buffer.from(buf1);buf1[0] = 0x61;// 输出: auffer
console.log(buf1.toString());// 输出: buffer
console.log(buf2.toString());

如果 buffer 不是一个 Buffer,则抛出 TypeError 错误。

(node:3872) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issue相关推荐

  1. 用Node.js申请缓存buffer报DEP0005错误的解决方法

    现象 用node.js写的程序中,有如下语句: var buf = new Buffer(myBase64Str, 'base64'); 其中,myBase64Str 是已有的一个base64字符串. ...

  2. Node.js缓冲模块Buffer

    前言 Javascript是为浏览器而设计的,能很好的处理unicode编码的字符串,但对于二进制或非unicode编码的数据就显得无能为力. Node.js继承Javascript的语言特性,同时又 ...

  3. Node的文件系统及Buffer概述

    目录 node.js开发网站 Node的文件系统 文件与目录的基本操作 写入文件操作 读文件操作 Node事件处理机制 Node的全局对象 Buffer node.js开发网站 node.js本身就是 ...

  4. 【DeprecationWarning: BICUBIC is deprecated and will be removed in Pillow 10 (2023-07-01).的解决方案】

    DeprecationWarning: BICUBIC is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampl ...

  5. DeprecationWarning: LANCZOS is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampl

    DeprecationWarning: LANCZOS is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampl ...

  6. DeprecationWarning: currentThread() is deprecated, use current_thread() instead

    项目场景: 使用pycharm编辑器使用python3.10 问题描述 DeprecationWarning: currentThread() is deprecated, use current_t ...

  7. DeprecationWarning: NEAREST is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampl

    消除警告 DeprecationWarning: NEAREST is deprecated and will be removed in Pillow 10 (2023-07-01). Use Re ...

  8. Pandas警告:DeprecationWarning: .ix is deprecated.(ix、loc、iloc的区别)

    ix和loc.iloc函数都是用来获取某一行或者某一列数据的. 请看如下案例: import pandas as pd data = [[1,2,3],[4,5,6],[7,8,9]] rows = ...

  9. Node.js 中的 Buffer 和字符编码

    众所周知,数据在网络中是通过二进制传输的,在 Node.js 中,Buffer 对象就是用于处理这些二进制数据.举个例子: console.log(Buffer.from('abcde')) 会输出: ...

  10. react+redux+node报错Tapable.plugin is deprecated. Use new API on `.h ooks` instead

    npm run hot运行 报错(node:5372) DeprecationWarning: Tapable.plugin is deprecated. Use new API on `.h ook ...

最新文章

  1. linux redhat 下让redis以服务方式运行
  2. 哈工大等提出轻量级盲超分辨模型LESRCNN,代码已开源
  3. 服务器怎么操作系统版本,服务器怎么操作系统版本
  4. Flutter 系列文章:Flutter Text 控件介绍
  5. PostgreSQL 辟谣存在任意代码执行漏洞:消息不实
  6. 双硬盘 双linux系统同时运行,XP与linux  双硬盘 双启动管理
  7. 力扣-387 字符串中的第一个唯一字符
  8. I - 数塔(动态规划)数塔问题
  9. tomcat内存溢出的解决方法
  10. 软考(中级-软件设计师)知识点整理
  11. UE4 实现C++蓝图接口
  12. Spring之bean标签属性详解
  13. 服务器503网页报错,网页为什么出现503错误?网页503错误的解决方法
  14. 欠款人署名故意签错,怎么维权
  15. Mybatis与springboot项目启动时出现Field mapper in ‘xxx‘ required a bean of type ‘xxx‘ that could not be found
  16. 77 关于 java.util.concurrent.RejectedExecutionException:Thread limit exceeded replacing blocked worker
  17. eclipse的启动失败提示“发生了错误,请参阅日志文件“
  18. python3中生成图片验证码和短信验证码的程序
  19. 【项目】问答系统-代码-后台
  20. 前2个月我国外贸进出口5919.9亿美元,下降11%,贸易逆差70.9亿美元

热门文章

  1. 执行stap测试例报错:“insmod: can‘t insert ‘xx.ko‘: invalid module format”
  2. DHCP与DHCP中继模式下获取IP地址
  3. 4. 嵌入式OpenWRT入门基础篇-----设置OpenWRT系统为AP、中继模式
  4. 【转载】装机知识显卡篇,一篇文章让小白透彻的了解显卡
  5. 数据结构:图结构的实现
  6. python xlsxwriter dict_Python玩转Excel神器xlsxwriter详解
  7. GIS小知识-由对GCJ-02的疑问引出的坐标系相关概念
  8. HG221GS光猫超级密码破解(烽火,华为)
  9. 深度学习入门(看了就会)
  10. JanusGraph安装和配置教程