Titanium SDK has full NodeJS support so users can use NPM modules Android and iOS platforms.
Minor differences
This implementation includes the NodeJS require algorithm with some minor differences:
- SDK doesn't attempt to load *.node files
- SDK doesn't have replacements for Node's core modules
- If the required string doesn't have the prefix of
./
,/
, or../
, and its not a native module, the SDK will fall back to legacy Titanium behavior of assuming the require is meant as "absolute" inside the app (as in starting at "Resources/
") - The SDK will load JSON files and directories (package.json's main property (look at it, resolve it, and try to load it), index.js, and index.json)
Algorithm summary
To summarize the algorithm the SDK uses, here are three summaries for requiring a module, loading as file, and loading as directory:
Require(X) from module at path Y
- If X is a core module,
- return the core module
- STOP
- If X begins with ./, or ../,
- LOAD_AS_FILE(Y + X)
- LOAD_AS_DIRECTORY(Y + X)
- If X begins with /,
- LOAD_AS_FILE(X)
- LOAD_AS_DIRECTORY(X)
- If X does not contain '/', assume it should try and load CommonJS module first....
- LOAD_AS_FILE(X/X.js): try to load "legacy" CommonJS file named
module.id/module.id.js
. - LOAD_AS_DIRECTORY(X): try to load CommonJS module as a directory
- LOAD_AS_FILE(X/X.js): try to load "legacy" CommonJS file named
- WARN user about possible bad require being treated as absolute THROW
"not found"
- LOAD_AS_FILE(X)
- LOAD_AS_DIRECTORY(X)
LOAD_AS_FILE(X)
- If X is a file, load X as JavaScript text or JavaScript Object (JSON). STOP
- If X.js is a file, load X.js as JavaScript text. STOP
- If X.json is a file, parse X.json to a JavaScript Object. STOP
LOAD_AS_DIRECTORY(X)
- If X/package.json is a file, If X/index.js is a file, load X/index.js
as JavaScript text. STOP
- Parse X/package.json and look for "main" field.
- let M = X + (json main field)
- LOAD_AS_FILE(M)
- If X/index.json is a file, parse X/index.json to a JavaScript object. STOP