1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
22 var NativeModule = require('native_module');
23 var util = NativeModule.require('util');
24 var runInThisContext = require('vm').runInThisContext;
25 var runInNewContext = require('vm').runInNewContext;
26 var assert = require('assert').ok;
27 var fs = NativeModule.require('fs');
30 // If obj.hasOwnProperty has been overridden, then calling
31 // obj.hasOwnProperty(prop) will break.
32 // See: https://github.com/joyent/node/issues/1707
33 function hasOwnProperty(obj, prop) {
34 return Object.prototype.hasOwnProperty.call(obj, prop);
38 function Module(id, parent) {
42 if (parent && parent.children) {
43 parent.children.push(this);
50 module.exports = Module;
52 // Set the environ variable NODE_MODULE_CONTEXTS=1 to make node load all
53 // modules in their own context.
54 Module._contextLoad = (+process.env['NODE_MODULE_CONTEXTS'] > 0);
56 Module._pathCache = {};
57 Module._extensions = {};
59 Module.globalPaths = [];
61 Module.wrapper = NativeModule.wrapper;
62 Module.wrap = NativeModule.wrap;
64 var path = NativeModule.require('path');
66 Module._debug = util.debuglog('module');
69 // We use this alias for the preprocessor that filters it out
70 var debug = Module._debug;
73 // given a module name, and a list of paths to test, returns the first
74 // matching file in the following precedence.
84 function statPath(path) {
86 return fs.statSync(path);
91 // check if the directory is a package.json dir
92 var packageMainCache = {};
94 function readPackage(requestPath) {
95 if (hasOwnProperty(packageMainCache, requestPath)) {
96 return packageMainCache[requestPath];
100 var jsonPath = path.resolve(requestPath, 'package.json');
101 var json = fs.readFileSync(jsonPath, 'utf8');
107 var pkg = packageMainCache[requestPath] = JSON.parse(json).main;
110 e.message = 'Error parsing ' + jsonPath + ': ' + e.message;
116 function tryPackage(requestPath, exts) {
117 var pkg = readPackage(requestPath);
119 if (!pkg) return false;
121 var filename = path.resolve(requestPath, pkg);
122 return tryFile(filename) || tryExtensions(filename, exts) ||
123 tryExtensions(path.resolve(filename, 'index'), exts);
126 // In order to minimize unnecessary lstat() calls,
127 // this cache is a list of known-real paths.
128 // Set to an empty object to reset.
129 Module._realpathCache = {};
131 // check if the file exists and is not a directory
132 function tryFile(requestPath) {
133 var stats = statPath(requestPath);
134 if (stats && !stats.isDirectory()) {
135 return fs.realpathSync(requestPath, Module._realpathCache);
140 // given a path check a the file exists with any of the set extensions
141 function tryExtensions(p, exts) {
142 for (var i = 0, EL = exts.length; i < EL; i++) {
143 var filename = tryFile(p + exts[i]);
153 Module._findPath = function(request, paths) {
154 var exts = Object.keys(Module._extensions);
156 if (request.charAt(0) === '/') {
160 var trailingSlash = (request.slice(-1) === '/');
162 var cacheKey = JSON.stringify({request: request, paths: paths});
163 if (Module._pathCache[cacheKey]) {
164 return Module._pathCache[cacheKey];
168 for (var i = 0, PL = paths.length; i < PL; i++) {
169 var basePath = path.resolve(paths[i], request);
172 if (!trailingSlash) {
173 // try to join the request to the path
174 filename = tryFile(basePath);
176 if (!filename && !trailingSlash) {
177 // try it with each of the extensions
178 filename = tryExtensions(basePath, exts);
183 filename = tryPackage(basePath, exts);
187 // try it with each of the extensions at "index"
188 filename = tryExtensions(path.resolve(basePath, 'index'), exts);
192 Module._pathCache[cacheKey] = filename;
199 // 'from' is the __dirname of the module.
200 Module._nodeModulePaths = function(from) {
201 // guarantee that 'from' is absolute.
202 from = path.resolve(from);
204 // note: this approach *only* works when the path is guaranteed
205 // to be absolute. Doing a fully-edge-case-correct path.split
206 // that works on both Windows and Posix is non-trivial.
207 var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\//;
209 var parts = from.split(splitRe);
211 for (var tip = parts.length - 1; tip >= 0; tip--) {
212 // don't search in .../node_modules/node_modules
213 if (parts[tip] === 'node_modules') continue;
214 var dir = parts.slice(0, tip + 1).concat('node_modules').join(path.sep);
222 Module._resolveLookupPaths = function(request, parent) {
223 if (NativeModule.exists(request)) {
224 return [request, []];
227 var start = request.substring(0, 2);
228 if (start !== './' && start !== '..') {
229 var paths = modulePaths;
231 if (!parent.paths) parent.paths = [];
232 paths = parent.paths.concat(paths);
234 return [request, paths];
237 // with --eval, parent.id is not set and parent.filename is null
238 if (!parent || !parent.id || !parent.filename) {
239 // make require('./path/to/foo') work - normally the path is taken
240 // from realpath(__filename) but with eval there is no filename
241 var mainPaths = ['.'].concat(modulePaths);
242 mainPaths = Module._nodeModulePaths('.').concat(mainPaths);
243 return [request, mainPaths];
246 // Is the parent an index module?
247 // We can assume the parent has a valid extension,
248 // as it already has been accepted as a module.
249 var isIndex = /^index\.\w+?$/.test(path.basename(parent.filename));
250 var parentIdPath = isIndex ? parent.id : path.dirname(parent.id);
251 var id = path.resolve(parentIdPath, request);
253 // make sure require('./path') and require('path') get distinct ids, even
254 // when called from the toplevel js file
255 if (parentIdPath === '.' && id.indexOf('/') === -1) {
259 debug('RELATIVE: requested:' + request +
260 ' set ID to: ' + id + ' from ' + parent.id);
262 return [id, [path.dirname(parent.filename)]];
266 // Check the cache for the requested file.
267 // 1. If a module already exists in the cache: return its exports object.
268 // 2. If the module is native: call `NativeModule.require()` with the
269 // filename and return the result.
270 // 3. Otherwise, create a new module for the file and save it to the cache.
271 // Then have it load the file contents before returning its exports
273 Module._load = function(request, parent, isMain) {
275 debug('Module._load REQUEST ' + (request) + ' parent: ' + parent.id);
278 var filename = Module._resolveFilename(request, parent);
280 var cachedModule = Module._cache[filename];
282 return cachedModule.exports;
285 if (NativeModule.exists(filename)) {
286 // REPL is a special case, because it needs the real require.
287 if (filename == 'repl') {
288 var replModule = new Module('repl');
289 replModule._compile(NativeModule.getSource('repl'), 'repl.js');
290 NativeModule._cache.repl = replModule;
291 return replModule.exports;
294 debug('load native module ' + request);
295 return NativeModule.require(filename);
298 var module = new Module(filename, parent);
301 process.mainModule = module;
305 Module._cache[filename] = module;
307 var hadException = true;
310 module.load(filename);
311 hadException = false;
314 delete Module._cache[filename];
318 return module.exports;
321 Module._resolveFilename = function(request, parent) {
322 if (NativeModule.exists(request)) {
326 var resolvedModule = Module._resolveLookupPaths(request, parent);
327 var id = resolvedModule[0];
328 var paths = resolvedModule[1];
330 // look up the filename first, since that's the cache key.
331 debug('looking for ' + JSON.stringify(id) +
332 ' in ' + JSON.stringify(paths));
334 var filename = Module._findPath(request, paths);
336 var err = new Error("Cannot find module '" + request + "'");
337 err.code = 'MODULE_NOT_FOUND';
344 // Given a file name, pass it to the proper extension handler.
345 Module.prototype.load = function(filename) {
346 debug('load ' + JSON.stringify(filename) +
347 ' for module ' + JSON.stringify(this.id));
349 assert(!this.loaded);
350 this.filename = filename;
351 this.paths = Module._nodeModulePaths(path.dirname(filename));
353 var extension = path.extname(filename) || '.js';
354 if (!Module._extensions[extension]) extension = '.js';
355 Module._extensions[extension](this, filename);
360 // Loads a module at the given file path. Returns that module's
361 // `exports` property.
362 Module.prototype.require = function(path) {
363 assert(util.isString(path), 'path must be a string');
364 assert(path, 'missing path');
365 return Module._load(path, this);
369 // Resolved path to process.argv[1] will be lazily placed here
370 // (needed for setting breakpoint when called with --debug-brk)
374 // Run the file contents in the correct scope or sandbox. Expose
375 // the correct helper variables (require, module, exports) to
377 // Returns exception, if any.
378 Module.prototype._compile = function(content, filename) {
381 content = content.replace(/^\#\!.*/, '');
383 function require(path) {
384 return self.require(path);
387 require.resolve = function(request) {
388 return Module._resolveFilename(request, self);
391 Object.defineProperty(require, 'paths', { get: function() {
392 throw new Error('require.paths is removed. Use ' +
393 'node_modules folders, or the NODE_PATH ' +
394 'environment variable instead.');
397 require.main = process.mainModule;
399 // Enable support to add extra extension types
400 require.extensions = Module._extensions;
401 require.registerExtension = function() {
402 throw new Error('require.registerExtension() removed. Use ' +
403 'require.extensions instead.');
406 require.cache = Module._cache;
408 var dirname = path.dirname(filename);
410 if (Module._contextLoad) {
411 if (self.id !== '.') {
412 debug('load submodule');
415 for (var k in global) {
416 sandbox[k] = global[k];
418 sandbox.require = require;
419 sandbox.exports = self.exports;
420 sandbox.__filename = filename;
421 sandbox.__dirname = dirname;
422 sandbox.module = self;
423 sandbox.global = sandbox;
426 return runInNewContext(content, sandbox, { filename: filename });
429 debug('load root module');
431 global.require = require;
432 global.exports = self.exports;
433 global.__filename = filename;
434 global.__dirname = dirname;
435 global.module = self;
437 return runInThisContext(content, { filename: filename });
440 // create wrapper function
441 var wrapper = Module.wrap(content);
443 var compiledWrapper = runInThisContext(wrapper, { filename: filename });
444 if (global.v8debug) {
446 // we enter the repl if we're not given a filename argument.
447 if (process.argv[1]) {
448 resolvedArgv = Module._resolveFilename(process.argv[1], null);
450 resolvedArgv = 'repl';
454 // Set breakpoint on module start
455 if (filename === resolvedArgv) {
456 global.v8debug.Debug.setBreakPoint(compiledWrapper, 0, 0);
459 var args = [self.exports, require, self, filename, dirname];
460 return compiledWrapper.apply(self.exports, args);
464 function stripBOM(content) {
465 // Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
466 // because the buffer-to-string conversion in `fs.readFileSync()`
467 // translates it to FEFF, the UTF-16 BOM.
468 if (content.charCodeAt(0) === 0xFEFF) {
469 content = content.slice(1);
475 // Native extension for .js
476 Module._extensions['.js'] = function(module, filename) {
477 var content = fs.readFileSync(filename, 'utf8');
478 module._compile(stripBOM(content), filename);
482 // Native extension for .json
483 Module._extensions['.json'] = function(module, filename) {
484 var content = fs.readFileSync(filename, 'utf8');
486 module.exports = JSON.parse(stripBOM(content));
488 err.message = filename + ': ' + err.message;
494 //Native extension for .node
495 Module._extensions['.node'] = process.dlopen;
498 // bootstrap main module.
499 Module.runMain = function() {
500 // Load the main module--the command line argument.
501 Module._load(process.argv[1], null, true);
502 // Handle any nextTicks added in the first tick of the program
503 process._tickCallback();
506 Module._initPaths = function() {
507 var isWindows = process.platform === 'win32';
510 var homeDir = process.env.USERPROFILE;
512 var homeDir = process.env.HOME;
515 var paths = [path.resolve(process.execPath, '..', '..', 'lib', 'node')];
518 paths.unshift(path.resolve(homeDir, '.node_libraries'));
519 paths.unshift(path.resolve(homeDir, '.node_modules'));
522 var nodePath = process.env['NODE_PATH'];
524 paths = nodePath.split(path.delimiter).concat(paths);
529 // clone as a read-only copy, for introspection.
530 Module.globalPaths = modulePaths.slice(0);
534 Module.requireRepl = function() {
535 return Module._load('repl', '.');
540 // backwards compatibility
541 Module.Module = Module;