require(X) from module at path Y
- If X is a core module, a. return the core module b. STOP
- If X begins with ‘/’ a. set Y to be the filesystem root
- If X begins with ‘./’ or ‘/’ or ‘../’ a. LOAD_AS_FILE(Y + X) b. LOAD_AS_DIRECTORY(Y + X)
- LOAD_NODE_MODULES(X, dirname(Y))
- THROW “not found”
LOAD_AS_FILE(X)
- If X is a file, load X as JavaScript text.
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
- If X.node is a file, load X.node as binary addon.
STOP
LOAD_INDEX(X)
- If X/index.js is a file, load X/index.js as JavaScript text.
STOP
- If X/index.json is a file, parse X/index.json to a JavaScript object.
STOP
- If X/index.node is a file, load X/index.node as binary addon.
STOP
LOAD_AS_DIRECTORY(X)
- If X/package.json is a file, a. Parse X/package.json, and look for “main” field. b. let M = X + (json main field) c. LOAD_AS_FILE(M) d. LOAD_INDEX(M)
- LOAD_INDEX(X)
LOAD_NODE_MODULES(X, START)
- let DIRS=NODE_MODULES_PATHS(START)
- for each DIR in DIRS: a. LOAD_AS_FILE(DIR/X) b. LOAD_AS_DIRECTORY(DIR/X)
NODE_MODULES_PATHS(START)
- let PARTS = path split(START)
- let I = count of PARTS - 1
- let DIRS = []
- while I >= 0, a. if PARTS[I] = “node_modules” CONTINUE b. DIR = path join(PARTS[0 .. I] + “node_modules”) c. DIRS = DIRS + DIR d. let I = I - 1
- return DIRS