node如何创建自定义模块

分为3步

1

先创建一个js文件,写入相关模块信息(方法、变量)

2

将模块进行导出操作

3

在新的js文件require之前创建的模块文件

  • 例如

    • 创建1.js

    let  name ={
      sayhello(){
          console.log("你好")
      }
    }
    ***导出操作****
    module.exports=name
    • 再创建2.js

    let Module=require('./1')
    console.log(Module)
    Module.sayhello()
    • 在终端输入

    node D:Goworkspacesrc odejs2.js

    输出为:

原文地址:https://www.cnblogs.com/liu-ai-yu/p/13087505.html