require('module') 是什么?

前端精髓

共 1698字,需浏览 4分钟

 ·

2022-01-07 21:42


在很多框架中经常会看到导入 module 模块来使用,那么导入的 module 模块和文件中的 module 变量到底又有什么区别呢?


const Module = require('module');

module 变量

在每个模块中,module 变量是对代表当前模块的对象的引用。

Module {  id: '.',  exports: 2,  parent: null,  filename: '/Users/leinov/github/node-api/module/module.js',  loaded: false,  children:   [ Module {       id: '/Users/leinov/github/node-api/module/circle.js',       exports: [Object],       parent: [Circular],       filename: '/Users/leinov/github/node-api/module/circle.js',       loaded: true,       children: [],       paths: [Array] } ],  paths:   [ '/Users/leinov/github/node-api/module/node_modules',     '/Users/leinov/github/node-api/node_modules',     '/Users/leinov/github/node_modules',     '/Users/leinov/node_modules',     '/Users/node_modules',     '/node_modules' ]


node 在运行某个模块时会生成一个 module 对象,关于 module 对象具体是怎么生成的,是通过 Module 这个构造函数生成实例对象。


function Module(id = '', parent) {  this.id = id;  this.path = path.dirname(id);  this.exports = {};  this.parent = parent;  updateChildren(parent, this, false);  this.filename = null;  this.loaded = false;  this.children = [];}
const module = new Module()


所以每个模块都是一个实例对象。那么如果我们想给实例对象添加公共方法,就要加在构造函数的原型上。


const Module = require('module');Module.prototype._compile = function () {}


当与 Module 的实例交互时提供通用的实用方法。其实源码中已经暴露了很多方法,比如 createRequire 方法,用于构造 require 函数的文件名,返回 require 函数。


const Module = require('module');const context = path.resolve('./src/package.json')const require = Module.createRequire(context)
// sibling-module.js 是 CommonJS 模块。const siblingModule = require('./sibling-module')


目的就是为了改变加载的位置,要从 src 这个目录里面加载 sibling-module 模块。


浏览 65
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报