美文网首页
node.js如何创建与调用自定义模块

node.js如何创建与调用自定义模块

作者: Oo晨晨oO | 来源:发表于2017-11-22 23:41 被阅读75次

创建模块

新建一个文件, 比如我在src文件夹里创建一个greeting.js

const hello = () => {
  console.log("hello~~!~");
}

module.exports.hello = hello

注意最后一句导出模块里的内容, 要用module.exports.xxx= xxx

调用模块

我要在index.js里面调用刚刚创建的greeting模块

const greeting = require('./src/greeting')

greeting.hello()

输出hello~~!~

相关文章

网友评论

      本文标题:node.js如何创建与调用自定义模块

      本文链接:https://www.haomeiwen.com/subject/vxdavxtx.html