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

2017-11-22  本文已影响75人  Oo晨晨oO

创建模块

新建一个文件, 比如我在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~~!~

上一篇 下一篇

猜你喜欢

热点阅读