Node.js - Built-in
Updated: 2020-06-29
process
process
is the global object, similar to window
in browser.
process.env
process.env
: environment variables defined and exported in ~/.bash_profile
or ~/.bashrc
process.argv
Stores commandline arguments. Try
console.log(process.argv)
from commandline
$ node test.js foo
[ 'node',
'/path/to/test.js',
'foo' ]
process.exit()
Exit program.
OS
Get num of CPUs:
var numCPUs = require("os").cpus().length;
JSON
JSON is exposed by V8
JSON.stringify(object)
: convert object to stringJSON.parse(string)
: convert string to object
To pretty print(indent by 4 spaces):
JSON.stringify(data, null, " ");
or
JSON.stringify(data, null, 4);
Others
require
, module
, and exports
.