【Node.js学习 day4——模块化】

模块化介绍

  1. 什么是模块化与模块?
    将一个复杂的程序文件依据一定规则(规范)拆分成多个文件的过程称之为模块化
    其中拆分的每个文件就是一个模块,模块的内部数据是私有的,不过模块可以暴露内部数据以便其他模块使用。
  2. 什么是模块化项目?
    编码时是按照模块一个一个编码的,整个项目就是一个模块化的项目
  3. 模块化好处
    · 防止命名冲突
    · 高复用性
    · 高维护性

模块化初体验

自定义模块 test.js

复制代码
function tiemo(){
    console.log('贴膜.........');
}

//暴露数据
module.exports = tiemo;

主程序index.js

复制代码
//引入自定义模块
const tiemo = require('./test.js');
//调用函数
tiemo();

调用:

模块暴露数据

模块暴露数据的方式有两种:

  1. module.exports = value
  2. exports.name = value

使用时有几点注意:

  • module.exports可以暴露任意数据

  • 不能使用exports = value 的形式暴露数据,模块内部module与exports的隐式关系exports = module.exports = {}

    代码示例:
    test.js

    function tiemo(){
    console.log('贴膜.........');
    }

    function niejiao(){
    console.log('捏脚.........');
    }

    //暴露数据
    module.exports = {tiemo,niejiao};

index.js

复制代码
//引入自定义模块
const me = require('./test.js');

console.log(me);

导入文件模块

在模块中使用require传入文件路径即可引入文件

复制代码
const test = require('./me.js');

require使用的一些注意事项:

  1. 对于自己创建的模块,导入时路径建议写相对路径,且不能省略./和.../

  2. js和json文件导入时可以不用写后缀,c/c++编写的node扩展文件也可以不写猴嘴,但是一般用不到

  3. 如果导入其他类型的文件,会以js文件进行处理

  4. 如果导入的路径是个文件夹,则会首先检测该文件夹下package.json文件中main属性对应的文件,如果main属性不存在,或者package.json不存在,则会检测文件夹下的index.js和index.json,如果还是没找到,就会报错。

  5. 导入node.js内置模块时,直接require模块的名字即可,无需加./和.../

    module.exports、exports以及require这些都是CommonJS模块化规范中的内容,而Node.js实现了CommonJS模块化规范。

require导入模块的基本流程

  1. 将相对路径转为绝对路径,定位目标文件

  2. 缓存检测

  3. 读取目标文件代码

  4. 包裹为一个函数并执行(自执行函数)。通过arguments.callee.toString()查看自执行函数

  5. 缓存模块的值

  6. 返回module.exports的值

    function require(file){
    //将相对路径转为绝对路径,定位目标文件
    let absolutePath = path.resolve(__dirname, file);
    //2.缓存监测
    if(caches[absolutePath]){
    return caches[absolutePath];
    }
    //3.读取文件的代码
    let code = fs.readFileSync(absolutePath).toString();
    //4.包裹为一个函数 然后执行
    let module = {};
    let exports = module.exports ={}
    (function (exports, require, module, __filename, __dirname){
    const test = {
    name:'尚硅谷'
    }

    复制代码
         module.exports = test;
    
         //输出
         console.log(arguments.callee.toString());
     })(exports, require, module, __filename, __dirname)
     //5.缓存结果
     caches[absolutePath] = module.exports;
     //6.返回module.exports的值
     return module.exports;

    }

    const m = require('./me.js');

CommonJS模块化规范

module.exports、exports以及require这些都是CommonJS模块化规范中的内容。而Node.js是实现了CommonJS模块化规范,二者关系有点像JavaScript与ECMAScript

相关推荐
WX-bisheyuange14 小时前
基于Spring Boot的社团管理系统的设计与实现
前端·javascript·vue.js·毕业设计
橙某人14 小时前
LogicFlow 插件魔改实录:手把手教你重写动态分组(DynamicGroup)🛠️
前端·javascript·vue.js
d111111111d14 小时前
STM32中USART和UART的区别是什么?
笔记·stm32·单片机·嵌入式硬件·学习
2501_9444460014 小时前
Flutter&OpenHarmony状态管理方案详解
开发语言·javascript·flutter
T_Donna14 小时前
【问题解决】react native: cli.init is not a function
javascript·react native·react.js
wdfk_prog14 小时前
[Linux]学习笔记系列 -- [fs]mbcache
linux·笔记·学习
心前阳光14 小时前
Unity通过ScriptableObject学习访问者模式
学习·unity·访问者模式
qx0914 小时前
html中使用vue3+elementplus
javascript·vue.js·html
Freshman小白15 小时前
《现代电力电子技术及应用》2025网课答案
学习·答案·网课答案
bjzhang7515 小时前
使用 HTML + JavaScript 实现滑动验证码
前端·javascript·html