1、用require()引入外部模块
- require可以传递一个文件的路径作为参数,node将会自动根据该路径来引入外部模块
ps.这里路径,如果使用相对路径,必须以.或…开头
2、用exports暴露内部数据或方法
- 只需要将需要暴露给外部的变量或方法设置为exports的属性即可
03.module
var md = require("./02.module");
console.log(md)
02.module
console.log("我是一个模块,我是02.module.js");
exports.x = "我是02.module.js中的x";
exports.y = "我是y";
exports.fn = function () {
};
结果

3、exports和module.exports
- exports:只能使用.的方式来向外暴露内部变量
exports.xxx = xxx - module.exports既可以通过.的形式,也可以直接赋值
module.exports.xxx = xxxx
module.exports = {}
expoerts.name = "猪八戒",
expoerts.age = 28,
expoerts.sayName = function () {
console.log("我是猪八戒");
}
nodule.exports = {
name:"猪八戒",
age:28,
sayName:function () {
console.log("我是猪八戒");
}