CommonJS Modules
CommonJS is the traditional Node.js module system. It uses require() to import modules and module.exports or exports to expose values.
CommonJS modules are evaluated and cached after their first load, so subsequent imports generally reuse the same module instance.
CommonJS Export and Import
const math = require('./math');
console.log(math.add(2, 3));💡 The application imports the exported object from
math.js using require().Key Takeaways
- CommonJS uses
require()andmodule.exports. - Loaded CommonJS modules are cached.
- Be consistent with the module system used by a project.
Knowledge Check
Which syntax is associated with CommonJS?