Both these require
statements appear to work the same way:
这两个需要语句似乎都以相同的方式工作:
var Mypackage = require('mypackage.js');
var Mypackage = require('mypackage.js');
var Mypackage require('mypackage');
var Mypackage require('mypackage');
Is there a difference between them?
它们之间有区别吗?
1 个解决方案
#1
13
Here is the answer:
这是答案:
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;
};
-
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. - Node.JS查看给定模块是否是核心模块。 (例如http,fs等)始终在加载模块中优先。
- 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 thenode_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 - 如果给定模块不是核心模块(例如http,fs等),则Node.js将开始搜索名为node_modules的目录。它将从当前目录(相对于Node.JS中当前正在执行的文件)开始,然后沿文件夹层次结构向上工作,检查node_modules文件夹的每个级别。一旦Node.JS找到node_modules文件夹,它就会尝试将给定模块加载为(.js)JavaScript文件或命名子目录;如果它找到了命名的子目录,它将尝试以各种方式加载文件。所以,例如
- 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 fornode_modules
andutils
in the following ways:./node_modules/utils.js
./node_modules/utils/index.js
./node_modules/utils/package.json
- 如果您请求加载模块,“utils”及其目录而不是.js文件,那么:Node.JS将通过以下方式在分层目录中搜索node_modules和utils:./ node_modules / utils.js ./ node_modules / utils / index.js ./node_modules/utils/package.json
- 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. - 如果Node.JS仍然无法在上面的步骤中找到该文件,Node.js将开始从您的机器上设置的环境变量(即NODE_PATH)开始查看目录路径(如果您在Windows上,显然由Node.JS安装程序文件设置) )在上述所有步骤中都找不到,然后将堆栈跟踪打印到stder Eg:错误:找不到模块'yourfile'有关更多信息:链接在这里,甚至循环require()也很好地解释了。
#1
13
Here is the answer:
这是答案:
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;
};
-
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. - Node.JS查看给定模块是否是核心模块。 (例如http,fs等)始终在加载模块中优先。
- 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 thenode_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 - 如果给定模块不是核心模块(例如http,fs等),则Node.js将开始搜索名为node_modules的目录。它将从当前目录(相对于Node.JS中当前正在执行的文件)开始,然后沿文件夹层次结构向上工作,检查node_modules文件夹的每个级别。一旦Node.JS找到node_modules文件夹,它就会尝试将给定模块加载为(.js)JavaScript文件或命名子目录;如果它找到了命名的子目录,它将尝试以各种方式加载文件。所以,例如
- 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 fornode_modules
andutils
in the following ways:./node_modules/utils.js
./node_modules/utils/index.js
./node_modules/utils/package.json
- 如果您请求加载模块,“utils”及其目录而不是.js文件,那么:Node.JS将通过以下方式在分层目录中搜索node_modules和utils:./ node_modules / utils.js ./ node_modules / utils / index.js ./node_modules/utils/package.json
- 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. - 如果Node.JS仍然无法在上面的步骤中找到该文件,Node.js将开始从您的机器上设置的环境变量(即NODE_PATH)开始查看目录路径(如果您在Windows上,显然由Node.JS安装程序文件设置) )在上述所有步骤中都找不到,然后将堆栈跟踪打印到stder Eg:错误:找不到模块'yourfile'有关更多信息:链接在这里,甚至循环require()也很好地解释了。