CommonJS
CommonJS 模块规范的导入导出方式与运行机制说明
#status / growing
#type / concept
[!info] related notes
- 所属 MOC: ecmascript-module-pattern, nodejs-moc
- 并列概念: es6-module-import-export
- Node 配置: package-json
CommonJS
在 Node.js 里,早期更常见的是 CommonJS。
导出:
// math.js
function add(a, b) {
return a + b;
}
module.exports = {
add
};
导入:
const math = require("./math");
console.log(math.add(1, 2));