當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


node.js process.mainModule用法及代碼示例


process.mainModule屬性是流程模塊的內置應用程序編程接口,用於獲取主模塊。這是獲取require.main的另一種方法,但與require.main不同,process.mainModule在運行時動態更改。通常,我們可以假定這兩個模塊相同。

用法:

process.mainModule

返回值:此屬性返回一個包含主模塊引用的對象。


以下示例說明了Node.js中process.mainModule屬性的用法:

範例1:

// Node.js program to demonstrate the 
// process.mainModule Property 
   
// Include process module 
const process = require('process'); 
  
// Printing process.mainModule property value 
console.log(process.mainModule);

輸出:

Module {
  id:'.',
  exports:{},
  parent:null,
  filename:'C:\\nodejs\\g\\process\\mainmodule_1.js',
  loaded:false,
  children:[],
  paths:
   [ 'C:\\nodejs\\g\\process\\node_modules',
     'C:\\nodejs\\g\\node_modules',
     'C:\\nodejs\\node_modules',
     'C:\\node_modules' 
   ]
}

範例2:

// Node.js program to demonstrate the 
// process.mainModule Property 
   
// Include process module 
const process = require('process'); 
  
// Printing process.mainModule property value 
var mainModule = process.mainModule; 
for(mod in mainModule) { 
    console.log(mod + ":" + mainModule[mod]); 
}

輸出:

id:.
exports:[object Object]
parent:null
filename:/home/cg/root/6720369/main.js
loaded:false
children:
paths:/home/cg/root/6720369/node_modules, /home/cg/root/node_modules,
 /home/cg/node_modules, /home/node_modules, /node_modules
load:function (filename) {
  debug('load %j for module %j', filename, this.id);

  assert(!this.loaded);
  this.filename = filename;
  this.paths = Module._nodeModulePaths(path.dirname(filename));

  var extension = path.extname(filename) || '.js';
  if (!Module._extensions[extension]) extension = '.js';
  Module._extensions[extension](this, filename);
  this.loaded = true;
}
require:function (path) {
  assert(path, 'missing path');
  assert(typeof path === 'string', 'path must be a string');
  return Module._load(path, this, /* isMain */ false);
}
_compile:function (content, filename) {
  // Remove shebang
  var contLen = content.length;
  if (contLen >= 2) {
    if (content.charCodeAt(0) === 35/*#*/ &&
        content.charCodeAt(1) === 33/*!*/) {
      if (contLen === 2) {
        // Exact match
        content = '';
      } else {
        // Find end of shebang line and slice it off
        var i = 2;
        for (; i < contLen; ++i) {
          var code = content.charCodeAt(i);
          if (code === 10/*\n*/ || code === 13/*\r*/)
            break;
        }
        if (i === contLen)
          content = '';
        else {
          // Note that this actually includes the newline character(s) in the
          // new output. This duplicates the behavior of the regular expression
          // that was previously used to replace the shebang line
          content = content.slice(i);
        }
      }
    }
  }

  // create wrapper function
  var wrapper = Module.wrap(content);

  var compiledWrapper = vm.runInThisContext(wrapper, {
    filename:filename,
    lineOffset:0,
    displayErrors:true
  });

  if (process._debugWaitConnect && process._eval == null) {
    if (!resolvedArgv) {
      // we enter the repl if we're not given a filename argument.
      if (process.argv[1]) {
        resolvedArgv = Module._resolveFilename(process.argv[1], null);
      } else {
        resolvedArgv = 'repl';
      }
    }

    // Set breakpoint on module start
    if (filename === resolvedArgv) {
      delete process._debugWaitConnect;
      const Debug = vm.runInDebugContext('Debug');
      Debug.setBreakPoint(compiledWrapper, 0, 0);
    }
  }
  var dirname = path.dirname(filename);
  var require = internalModule.makeRequireFunction.call(this);
  var args = [this.exports, require, this, filename, dirname];
  var depth = internalModule.requireDepth;
  if (depth === 0) stat.cache = new Map();
  var result = compiledWrapper.apply(this.exports, args);
  if (depth === 0) stat.cache = null;
  return result;
}

注意:上麵的程序將通過使用node filename.js命令。

參考: https://nodejs.org/api/process.html#process_process_mainmodule



相關用法


注:本文由純淨天空篩選整理自anwesha0107大神的英文原創作品 Node.js | process.mainModule Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。