Node.js 进程对象
进程对象是一个全局对象,因此可以从任何地方访问它。因为它是一个预定义的库,所以我们不必在全局范围内将它下载到我们的系统中。
先决条件:
- Node基础知识
- 已安装 Node.js(版本 12+)
- 已安装 NPM(版本 6+)
需要模块:您可以使用以下代码包含模块:
var process = require('process');
注意:它是一个全局对象,所以不需要显式安装它。
示例 1:创建一个 JavaScript 文件index.js并记下以下代码:
index.js
// Including the module into out project
var process = require('process');
// It will return the current working directory
console.log('this is the working directory --> ' + process.cwd());
// It will return the version of process we are using
console.log('this is the process version --> ' + process.version);
// It will return the type of OS we are using at that time.
console.log('current OS we are using --> ' + process.platform);
index.js
// Including the module into out project
var process = require('process');
// It will return the Feature Object
console.log('Feature Property: ', process.features);
使用以下命令运行index.js文件:
node index.js
输出:
示例 2:创建一个 JavaScript 文件index.js并记下以下代码:
index.js
// Including the module into out project
var process = require('process');
// It will return the Feature Object
console.log('Feature Property: ', process.features);
使用以下命令运行index.js文件:
node index.js
输出:
Feature Property: {
inspector: true,
debug: false,
uv: true,
ipv6: true,
tls_alpn: true,
tls_sni: true,
tls_ocsp: true,
tls: true,
cached_builtins: true
}
参考: https://nodejs.org/api/process.html#process_process