Node.js - 模块化

108 阅读5分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第2天,点击查看活动详情

什么是模块化

  • 模块化是指解决一个复杂问题时,自顶向下逐层把系统划分为若干模块的过程,对于整个系统来说,模块是可组合、分解和更换的单元

  • 编程领域中的模块化,就是遵守固定的规则,把一个大文件拆成独立并互相依赖的多个小模块

  • 把代码进行模块化拆分的好处

    • 提高了代码的复用性
    • 提高了代码的可维护性
    • 可以实现按需加载

Node.js 中模块化的分类

Node.js 中根据模块来源的不同,将模块分为了三大类,分别是

  • 内置模块: 内置模块是由 Node.js 官方提供的,如 fs、path、http 等
  • 自定义模块: 用户创建的每个 .js 文件,都是自定义模块
  • 第三方模块: 有三方公司开发出来的模块,并非官方提供的内置模块,也不是用户自己创建的自定义模块,使用前需要下载

Node.js 中模块的加载

  • 使用 require() 方法,可以加载需要的内置模块、用户自定义模块、第三方模块进行使用

    // 加载内置模块
    const fs = require('fs')
    
    // 加载用户的自定义模块(输入内容是个路径)
    const custom = require('./custom.js')
    
    // 加载第三方模块
    const moment = require('moment')
    
  • 使用 require() 方法加载自定义模块,可以省略自定义模块路径中 .js 的后缀名

    const custom = require('./custom.js')
    const custom = require ('./custom')
    
  • 使用 require() 方法加载其他模块时,会执行被加载模块中的代码

    自定义模块 A

    const name = 'customA'
    console.log('我是 Custom A')
    

    自定义模块调用

    const customA = require ('./customA')
    console.log(customA)
    
    log:(log 中会先输出 customA 中的代码,之后再输出模块B 中的log,为空对象)
    我是 Custom A
    {}
    

Node.js 中模块的作用域

  • 模块的作用域: 和函数作用域类似,在自定义模块中定义的变量、方法等成员,只能在当前模块内被访问,这种模块级别的访问限制叫做模块作用域

    自定义模块 A

    // 定义常量 name
    const name = 'A'
    
    // 定义函数 sayHi ()
    function sayHi (){
        console.log('大家好,我是A')
    }
    

    自定义模块调用

    const customA = require ('./customA')
    console.log(customA)
    
    log:(输出空对象,在模块B中无法访问到 customA 模块中的私有成员)
    {}
    
  • 模块作用域的好处

    防止全局变量污染的问题


向外共享模块作用域的成员

module 对象

  • 在每个 .js 自定义模块中都有一个 module 对象,它里面存储了和当前模块有关信息

  • 在 .js 文件中打印 module

    Snip20220324_32.png

module.exports 对象

  • module 对象中,有一个 exports 属性,默认是空对象。可以使用 module.exports 对象,将模块内的成员共享出去,供外界使用

  • 外界使用 require() 方法导入自定义模块时,得到的就是 module.exports 所指向的对象

    自定义模块 custom

    // 定义常量 name
    const name = 'custom'
    
    // 定义函数 sayHi ()
    function sayHi (){
        console.log('大家好,我是custom')
    }
    
    // 向 module.exports 对象上挂载 name 属性
    module.exports.name = name
    
    // 向 module.exports 对象上挂载 sayHi 方法
    module.exports.sayHi = sayHi
    

    自定义模块调用

    const custom = require('./custom.js')
    console.log(custom)
    
    log:
    { name: 'customA', sayHi: [Function: sayHi] }
    

共享成员时的注意点

  • 使用 require() 方法导入模块时,导入结果,永远以 module.exports 指向的对象为准(后面的赋值会影响前面的赋值)

    自定义模块 custom

    // 定义常量 name
    const name = 'customA'
    
    // 定义函数 sayHi ()
    function sayHi (){
        console.log('大家好,我是A')
    }
    
    // 向 module.exports 对象上挂载 name 属性
    module.exports.name = name
    
    // 向 module.exports 对象上挂载 sayHi 方法
    module.exports.sayHi = sayHi
    
    
    // 向 module.exports 对象上挂载一个新对象
    module.exports = {
        nikename: 'customB',
        sayHello() {
            console.log('sayHello')
        }
    }
    

    自定义模块调用

    const custom = require('./custom.js')
    console.log(custom)
    
    log:
    { nikename: 'customB', sayHello: [Function: sayHello] }
    

exports 对象

由于 module.exports 单词写起来比较复杂,为了简化向外共享成员的代码, Node 提供了 exports 对象。默认情况下,exports 和 module.exports 指向同一个对象。最终共享的结果,还是以 module.exports 指向的对象为准

自定义模块 custom

const name = 'customA'

function sayHi (){
    console.log('大家好,我是A')
}

//分别挂载在 module.exports 和 exports 上
module.exports.name = name
exports.sayHi = sayHi

自定义模块调用

const custom = require('./custom.js')
console.log(custom)

log:
{ name: 'customA', sayHi: [Function: sayHi] }

exports 和 module.exports 的使用误区

  • 为了防止混乱,建议不要在同一个模块中同时使用 exportsmodule.exports

  • require() 模块时,得到的永远是 module.exports 指向的对象

    • 示例1(log 中并不会打印出 name 属性,因为永远以 module.exports 指向为准,module.exports 被赋值了新对象)

      //自定义customA
      const name = 'customA'
      const age = 18
      exports.name = name
      module.exports = {
          age : age
      }
      
      //自定义模块调用 
      const custom = require('./custom.js')
      console.log(custom)
      
      log:
      { age: 18 }
      
    • 示例2(log 中并不会打印出 age 属性,虽然 exports 被赋值了新对象,但是永远以 module.exports 指向为准)

      //自定义customA
      const name = 'customA'
      const age = 18
      module.exports.name = name
      exports = {
          age : age
      }
      
      //自定义模块调用 
      const custom = require('./custom.js')
      console.log(custom)
      
      log:
      { name: 'customA' }
      
    • 示例3 (log 中都会打印,因为在自定义 customA 中,并没有给 module.export 赋值新对象)

      //自定义customA
      const name = 'customA'
      const age = 18
      module.exports.name = name
      exports.age = age
      
      //自定义模块调用 
      const custom = require('./custom.js')
      console.log(custom)
      
      log:
      { name: 'customA', age: 18 }
      
    • 示例4 (log 中都会打印,因为在自定义 customA 中,并没有给 module.export 赋值新对象)

      //自定义customA
      const name = 'customA'
      const age = 18
      
      exports = {
          age : age
      }
      
      module.exports = exports
      module.exports.name = name
      
      //自定义模块调用 
      const custom = require('./custom.js')
      console.log(custom)
      
      log:
      { age: 18, name: 'customA' }
      

Node.js 中的模块化规范

  • Node.js 遵循了 CommonJS 模块化规范,CommonJS 规定了模块化的特性和各模块之间相互依赖

  • CommonJS 规定:

    • 每个模块内部,module 变量代码了当前模块
    • module 变量是一个对象,它的 exports 属性(即 module.exports)是对外的接口
    • 加载某个模块,其实就是加载该模块的 module.exports 属性,require() 方法用于加载模块