require('mypackage.js')和require('mypackage')有什么区别? [英] What is the difference between require('mypackage.js') and require('mypackage')?

查看:151
本文介绍了require('mypackage.js')和require('mypackage')有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两个require语句的工作方式似乎相同:

Both these require statements appear to work the same way:

var Mypackage = require('mypackage.js');

var Mypackage require('mypackage');

它们之间有区别吗?

推荐答案

以下是答案:

Module.prototype.load = function(filename) {
  debug('load ' + JSON.stringify(filename) +
        ' for module ' + JSON.stringify(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;
};

  1. Node.JS 可以查看给定的模块是否为核心模块. (例如httpfs等) 在加载模块中始终优先.
  2. 如果给定的模块不是核心模块(例如httpfs等),则Node.js将开始搜索名为 node_modules 的目录.
    它将从当前目录开始(相对于 Node.JS 中当前正在执行的文件),然后逐步向上移动文件夹层次结构,检查每个级别的node_modules文件夹. 一旦 Node.JS 找到node_modules文件夹,它将尝试将给定模块加载为(.js) JavaScript文件或命名子目录;如果找到命名的子目录,它将尝试以各种方式加载文件.因此,例如
  3. 如果您请求加载模块"utils"及其目录而不是.js文件,则:
    节点.JS 将在分层目录中搜索node_modules,然后 utils通过以下方式:
    ./node_modules/utils.js
    ./node_modules/utils/index.js
    ./node_modules/utils/package.json
  4. 如果 Node.JS 在上述步骤中仍然找不到文件,则Node.js将开始调查环境变量的目录路径,即在计算机上设置的 NODE_PATH (如果在Windows上,显然由Node.JS安装程序文件设置) 然后,在上述所有步骤中均未找到,则将堆栈跟踪记录打印到 stder
    例如: Error: Cannot find module 'yourfile'
    有关更多信息:链接是此处,甚至是循环的require() 解释得很好.
  1. Node.JS looks to see if the given module is a core module. (e.g. http, fs, etc.) Always takes the precedence in the loading modules.
  2. If the given module is not a core module (e.g. http, fs, etc.), Node.js will then begin to search for a directory named, node_modules.
    It will start in the current directory (relative to the currently-executing file in Node.JS) and then work its way up the folder hierarchy, checking each level for a node_modules folder. Once Node.JS finds the node_modules folder, it will then attempt to load the given module either as a (.js) JavaScript file or as a named sub-directory; if it finds the named sub-directory, it will then attempt to load the file in various ways. So, for example
  3. If you make a request to load the module, "utils" and its a directory not a .js file then:
    Node.JS will search a hierarchical directory for node_modules and utils in the following ways:
    ./node_modules/utils.js
    ./node_modules/utils/index.js
    ./node_modules/utils/package.json
  4. If Node.JS still can't find the file in above steps, Node.js will then start to look into the directory paths from environment variables i.e. NODE_PATH set on your machine(obviously set by Node.JS installer file if you are on windows) Not Found in all the above steps then, prints a stack trace to stder
    E.g.: Error:Cannot find module 'yourfile'
    For more information: link is here even the cyclic require() is explained very well.

这篇关于require('mypackage.js')和require('mypackage')有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆