‘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
相關用法
- Node.js process.nextTick()用法及代碼示例
- jQuery UI dialog create(event, ui)用法及代碼示例
- jQuery UI dialog close(event, ui)用法及代碼示例
- jQuery UI dialog resizeStart(event, ui)用法及代碼示例
- jQuery UI dialog dragStop(event, ui)用法及代碼示例
- jQuery UI dialog dragStart(event, ui)用法及代碼示例
- jQuery UI dialog resize(event,ui)用法及代碼示例
- jQuery UI dialog open(event,ui)用法及代碼示例
- jQuery UI dialog focus(event,ui)用法及代碼示例
- Node.js Process exit用法及代碼示例
- Node.js Process message用法及代碼示例
- Node.js Process beforeExit用法及代碼示例
- Node.js Process uncaughtException用法及代碼示例
- Node.js Process multipleResolves用法及代碼示例
注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Node.js Process warning Event。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。