步骤
- 创建npm.js并书写模块
- 创建index.js,调用npm.js
- 执行命令
代码
// 创建npm.js并书写模块
// npm.js
// 注意:这里用立即函数调用,是为了隐藏私有变量,只有在返回指定的变量或函数。
module.exports = (function (){
const echo = function(str){
console.log(str)
}
return {
echo: echo
}
})()
// 创建index.js,调用npm.js
const npm = require('./npm.js')
npm.echo('test string')
node index.js