Use Node.js in your end devices(QQ: 796448809)
The following shows module APIs available for each platform.
Linux (Ubuntu) |
Raspbian (Raspberry Pi) |
NuttX (STM32F4-Discovery) |
TizenRT (Artik053) |
|
---|---|---|---|---|
require | O | O | O | O |
The require
function is always available there is no need to import module
explicitly.
id
{string} Module name to be loaded.Loads the module named id
.
Example
var assert = require('assert');
assert.equal(2, 2);
Modules are cached in this object when they are required. By deleting a key value from this object, the next require will reload the module.
Loading a module
If a native module named id
exists, load it and return.
(Native module: which module came from the IoT.js itself)
require
function searches for modules in the following order:
$NODE_PRIORITIZED_PATH
if present.node_modules
folder under current working directory.$NODE_PATH
if present.$HOME/node_modules
.For each directory in search paths above:
id
exists, load it and return.id.js
exists, load it and retun.id
exists, module system consider the directory as a package:
id/package.json
contains main property, load the file named main property.id/package.json
exists, but neither the main property nor the file named main property exist, load index.js
.When a file is run directly, require.main
is set to its module. That means that it is possible to determine whether a file has been run directly by testing require.main === module
. For a file foo.js, this will be true if run via cmd directly, but false if run by require('./foo')
, because module
object is set to the foo
module but require.main
is still your entry file module. So you can get the entry point of the current application from checking require.main.filename
anywhere.