Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 禁止使用'Buffer()'警告_Node.js - Fatal编程技术网

Node.js 禁止使用'Buffer()'警告

Node.js 禁止使用'Buffer()'警告,node.js,Node.js,如何仅抑制Node.js中已弃用的Buffer()警告,而不丢失其他警告 ERROR (node:8) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead. 我知道Buffer()已被弃用,我

如何仅抑制Node.js中已弃用的
Buffer()警告,而不丢失其他警告

ERROR   (node:8) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
我知道
Buffer()
已被弃用,我的代码中没有一个使用它,但我的依赖关系一直在使用它,我无法立即解决这个问题,所以我想让警告静音,因为它们会自动发送到Slack,这使得我的应用程序很难看到真正的错误

我试过:

process.on('warning',(warning)=>{
如果(!warning.message.includes('Buffer()已弃用')){
控制台。错误(警告);
}
});
但这没有效果。我猜这个通知是通过其他机制发送的


我已经研究了修复所有调用
new Buffer()
的依赖项所需的步骤,但是有很多,一些已经多年没有接受PRs,主分支中的测试失败等等,所以这不是一件容易的事情。此外,所有PRs都需要时间才能被接受、合并,并在整个链条中占据一席之地。同时,我不希望每30秒就有一次警报,因为我对此无能为力。

这段猴子补丁代码将使特定警告静音:

const origWarning = process.emitWarning;
process.emitWarning = function(...args) {
    if (args[2] !== 'DEP0005') {
        // pass any other warnings through normally
        return origWarning.apply(process, args);
    } else {
        // do nothing, eat the warning
    }
}
显然,必须在生成您试图抑制的警告的代码运行之前运行此代码


仅供参考,为了解决这个问题,我采取了以下步骤:

  • 我看了一下,看看警告是在哪里产生的,是如何产生的
  • 在这段代码中,我可以看到它调用了
    process.emitWarning(bufferWarning,'DeprecationWarning','DEP0005')
  • 因此,我为
    process.emitWarning()
    制作了一个猴子补丁,它查找第三个参数是
    'DEP0005'
    ,这是特定于此特定警告的
  • 然后,我在缓冲区警告和另一个警告上进行了测试,以确保缓冲区警告被阻止,而另一个警告没有被阻止
  • 然后,我检查了
    process.emitWarning()
    的实现,以查看是否有任何标志或其他设置可以仅抑制该特定警告,以防有支持的或更好的方法来执行此操作。没有找到这样的选项
  • 确认
    DEP0005
    仅用于此特定警告,并在nodejs存储库和中进行搜索

  • 这段猴子补丁代码将使特定警告静音:

    const origWarning = process.emitWarning;
    process.emitWarning = function(...args) {
        if (args[2] !== 'DEP0005') {
            // pass any other warnings through normally
            return origWarning.apply(process, args);
        } else {
            // do nothing, eat the warning
        }
    }
    
    显然,必须在生成您试图抑制的警告的代码运行之前运行此代码


    仅供参考,为了解决这个问题,我采取了以下步骤:

  • 我看了一下,看看警告是在哪里产生的,是如何产生的
  • 在这段代码中,我可以看到它调用了
    process.emitWarning(bufferWarning,'DeprecationWarning','DEP0005')
  • 因此,我为
    process.emitWarning()
    制作了一个猴子补丁,它查找第三个参数是
    'DEP0005'
    ,这是特定于此特定警告的
  • 然后,我在缓冲区警告和另一个警告上进行了测试,以确保缓冲区警告被阻止,而另一个警告没有被阻止
  • 然后,我检查了
    process.emitWarning()
    的实现,以查看是否有任何标志或其他设置可以仅抑制该特定警告,以防有支持的或更好的方法来执行此操作。没有找到这样的选项
  • 确认
    DEP0005
    仅用于此特定警告,并在nodejs存储库和中进行搜索

  • 这将捕获事件,但仍将打印。您可以全局使用--no-deprecation。这是在AWS Lambda中运行的,因此我认为无法将CLI标志传递给
    节点
    进程。此外,我只想抑制
    缓冲区
    弃用。我想记录任何其他警告。这将捕获事件,但仍将打印。您可以全局使用--no-deprecation。这是在AWS Lambda中运行的,因此我认为无法将CLI标志传递给
    节点
    进程。此外,我只想抑制
    缓冲区
    弃用。我想记录任何其他警告。