当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Node.js Process warning用法及代码示例


‘warning’是进程模块中的Process类的事件,只要Node.js发出进程警告,该事件就会发出。

用法:

Event:'warning'

参数:此事件不接受任何参数作为参数。

返回值:此事件只返回回调函数以进行进一步的操作。



范例1:

index.js


// Node.js program to demonstrate the  
// Process 'warning' Event
  
// Importing process module
const process = require('process');
  
// Intentionally emited warning
process.emitWarning('something strange happened');
  
// Event 'warning' 
process.on('warning', (warning) => {
   console.warn("warning name - " + warning.name);
   console.warn("warning message - " + warning.message);
});

使用以下命令运行index.js文件:

node index.js

输出:

(node:8004) Warning:something strange happened
(Use `node --trace-warnings ...` to show where
the warning was created)
warning name - Warning
warning message - something strange happened

范例2:

index.js


// Node.js program to demonstrate the  
// Process 'warning' Event
  
// Importing process module
const process = require('process');
  
// Intentionally emited warning
process.emitWarning('Running out of Storage');
  
// Event 'warning' 
process.on('warning', (warning) => {
  console.warn("warning stacktrace - " + warning.stack)
});

使用以下命令运行index.js文件:

node index.js

输出:

(node:13400) Warning:Running out of Storage
(Use `node -trace-warnings …` to show where the warning was created)
warning stacktrace - Warning:Running out of Storage
at Object.<anonymous> (F:\java\GFG.js:8:9)
at Module._compile (node:internal/modules/cjs/loader:1109:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1138:10)
at Module.load (node:internal/modules/cjs/loader:989:32)
at Function.Module._load (node:internal/modules/cjs/loader:829:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47

参考:https://nodejs.org/dist/latest-v16.x/docs/api/process.html#process_event_warning

相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 Node.js Process warning Event。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。