require()
函数加载的都是模块module.exports
require()
来加载,那么就不就叫模块。比如有些包中没有设置启动文件(package.json中的main字段),就不是模块。require('标识符')
来require('模块名称')
require('./a/b.js')
通过指定相对路径来加载模块require('/a/b.js')
或 require('c:\a\b.js')
通过指定绝对路径来加载require('')
加载模块的时候,相对路径永远相对于当前模块,不受node命令执行的路径影响。console.log(module.paths);
来查看exports = module.exports;
// To illustrate(说明) the behavior, imagine this hypothetical implementation of require(), which is quite similar to what is actually done by require():
function require(...) {
var module = { exports: {} };
((module, exports) => {
// Your module code here. In this example, define a function.
function some_func() {};
exports = some_func;
// At this point, exports is no longer a shortcut to module.exports, and
// this module will still export an empty default object.
module.exports = some_func;
// At this point, the module will now export some_func, instead of the
// default object.
})(module, module.exports);
return module.exports;
}
"use strict";
或 'use strict';